1   -   I n t r o d u c t i o n 
 
 L u a   i s   a n   e x t e n s i o n   p r o g r a m m i n g   l a n g u a g e   d e s i g n e d   t o   s u p p o r t   g e n e r a l   p r o c e d u r a l   p r o g r a m m i n g   w i t h   d a t a   d e s c r i p t i o n   
 
 f a c i l i t i e s .   I t   a l s o   o f f e r s   g o o d   s u p p o r t   f o r   o b j e c t - o r i e n t e d   p r o g r a m m i n g ,   f u n c t i o n a l   p r o g r a m m i n g ,   a n d   d a t a - d r i v e n   
 
 p r o g r a m m i n g .   L u a   i s   i n t e n d e d   t o   b e   u s e d   a s   a   p o w e r f u l ,   l i g h t - w e i g h t   s c r i p t i n g   l a n g u a g e   f o r   a n y   p r o g r a m   t h a t   n e e d s   o n e .   L u a   
 
 i s   i m p l e m e n t e d   a s   a   l i b r a r y ,   w r i t t e n   i n   c l e a n   C   ( t h a t   i s ,   i n   t h e   c o m m o n   s u b s e t   o f   A N S I   C   a n d   C + + ) .   
 
 
 
 B e i n g   a n   e x t e n s i o n   l a n g u a g e ,   L u a   h a s   n o   n o t i o n   o f   a   " m a i n "   p r o g r a m :   i t   o n l y   w o r k s   e m b e d d e d   i n   a   h o s t   c l i e n t ,   c a l l e d   t h e   
 
 e m b e d d i n g   p r o g r a m   o r   s i m p l y   t h e   h o s t .   T h i s   h o s t   p r o g r a m   c a n   i n v o k e   f u n c t i o n s   t o   e x e c u t e   a   p i e c e   o f   L u a   c o d e ,   c a n   w r i t e   a n d   
 
 r e a d   L u a   v a r i a b l e s ,   a n d   c a n   r e g i s t e r   C   f u n c t i o n s   t o   b e   c a l l e d   b y   L u a   c o d e .   T h r o u g h   t h e   u s e   o f   C   f u n c t i o n s ,   L u a   c a n   b e   
 
 a u g m e n t e d   t o   c o p e   w i t h   a   w i d e   r a n g e   o f   d i f f e r e n t   d o m a i n s ,   t h u s   c r e a t i n g   c u s t o m i z e d   p r o g r a m m i n g   l a n g u a g e s   s h a r i n g   a   
 
 s y n t a c t i c a l   f r a m e w o r k .   T h e   L u a   d i s t r i b u t i o n   i n c l u d e s   a   s a m p l e   h o s t   p r o g r a m   c a l l e d   l u a ,   w h i c h   u s e s   t h e   L u a   l i b r a r y   t o   o f f e r   
 
 a   c o m p l e t e ,   s t a n d - a l o n e   L u a   i n t e r p r e t e r .   
 
 
 
 L u a   i s   f r e e   s o f t w a r e ,   a n d   i s   p r o v i d e d   a s   u s u a l   w i t h   n o   g u a r a n t e e s ,   a s   s t a t e d   i n   i t s   l i c e n s e .   T h e   i m p l e m e n t a t i o n   d e s c r i b e d   
 
 i n   t h i s   m a n u a l   i s   a v a i l a b l e   a t   L u a ' s   o f f i c i a l   w e b   s i t e ,   w w w . l u a . o r g .   
 
 
 
 L i k e   a n y   o t h e r   r e f e r e n c e   m a n u a l ,   t h i s   d o c u m e n t   i s   d r y   i n   p l a c e s .   F o r   a   d i s c u s s i o n   o f   t h e   d e c i s i o n s   b e h i n d   t h e   d e s i g n   o f   
 
 L u a ,   s e e   t h e   t e c h n i c a l   p a p e r s   a v a i l a b l e   a t   L u a ' s   w e b   s i t e .   F o r   a   d e t a i l e d   i n t r o d u c t i o n   t o   p r o g r a m m i n g   i n   L u a ,   s e e   R o b e r t o ' s   
 
 b o o k ,   P r o g r a m m i n g   i n   L u a   ( S e c o n d   E d i t i o n ) .   
 
 
 
 2   -   T h e   L a n g u a g e 
 
 T h i s   s e c t i o n   d e s c r i b e s   t h e   l e x i s ,   t h e   s y n t a x ,   a n d   t h e   s e m a n t i c s   o f   L u a .   I n   o t h e r   w o r d s ,   t h i s   s e c t i o n   d e s c r i b e s   w h i c h   t o k e n s   
 
 a r e   v a l i d ,   h o w   t h e y   c a n   b e   c o m b i n e d ,   a n d   w h a t   t h e i r   c o m b i n a t i o n s   m e a n .   
 
 
 
 T h e   l a n g u a g e   c o n s t r u c t s   w i l l   b e   e x p l a i n e d   u s i n g   t h e   u s u a l   e x t e n d e d   B N F   n o t a t i o n ,   i n   w h i c h   { a }   m e a n s   0   o r   m o r e   a ' s ,   a n d   [ a ]   
 
 m e a n s   a n   o p t i o n a l   a .   N o n - t e r m i n a l s   a r e   s h o w n   l i k e   n o n - t e r m i n a l ,   k e y w o r d s   a r e   s h o w n   l i k e   k w o r d ,   a n d   o t h e r   t e r m i n a l   s y m b o l s   
 
 a r e   s h o w n   l i k e   ` =  .   T h e   c o m p l e t e   s y n t a x   o f   L u a   c a n   b e   f o u n d   i n    8   a t   t h e   e n d   o f   t h i s   m a n u a l .   
 
 
 
 2 . 1   -   L e x i c a l   C o n v e n t i o n s 
 
 N a m e s   ( a l s o   c a l l e d   i d e n t i f i e r s )   i n   L u a   c a n   b e   a n y   s t r i n g   o f   l e t t e r s ,   d i g i t s ,   a n d   u n d e r s c o r e s ,   n o t   b e g i n n i n g   w i t h   a   d i g i t .   
 
 T h i s   c o i n c i d e s   w i t h   t h e   d e f i n i t i o n   o f   n a m e s   i n   m o s t   l a n g u a g e s .   ( T h e   d e f i n i t i o n   o f   l e t t e r   d e p e n d s   o n   t h e   c u r r e n t   l o c a l e :   a n y   
 
 c h a r a c t e r   c o n s i d e r e d   a l p h a b e t i c   b y   t h e   c u r r e n t   l o c a l e   c a n   b e   u s e d   i n   a n   i d e n t i f i e r . )   I d e n t i f i e r s   a r e   u s e d   t o   n a m e   v a r i a b l e s   
 
 a n d   t a b l e   f i e l d s .   
 
 
 
 T h e   f o l l o w i n g   k e y w o r d s   a r e   r e s e r v e d   a n d   c a n n o t   b e   u s e d   a s   n a m e s :   
 
 
 
           a n d               b r e a k           d o                 e l s e             e l s e i f 
 
           e n d               f a l s e           f o r               f u n c t i o n     i f 
 
           i n                 l o c a l           n i l               n o t               o r 
 
           r e p e a t         r e t u r n         t h e n             t r u e             u n t i l           w h i l e 
 
 
 
 L u a   i s   a   c a s e - s e n s i t i v e   l a n g u a g e :   a n d   i s   a   r e s e r v e d   w o r d ,   b u t   A n d   a n d   A N D   a r e   t w o   d i f f e r e n t ,   v a l i d   n a m e s .   A s   a   c o n v e n t i o n ,   
 
 n a m e s   s t a r t i n g   w i t h   a n   u n d e r s c o r e   f o l l o w e d   b y   u p p e r c a s e   l e t t e r s   ( s u c h   a s   _ V E R S I O N )   a r e   r e s e r v e d   f o r   i n t e r n a l   g l o b a l   
 
 v a r i a b l e s   u s e d   b y   L u a .   
 
 
 
 T h e   f o l l o w i n g   s t r i n g s   d e n o t e   o t h e r   t o k e n s :   
 
 
 
           +           -           *           /           %           ^           # 
 
           = =         ~ =         < =         > =         <           >           = 
 
           (           )           {           }           [           ] 
 
           ;           :           ,           .           . .         . . . 
 
 
 
 L i t e r a l   s t r i n g s   c a n   b e   d e l i m i t e d   b y   m a t c h i n g   s i n g l e   o r   d o u b l e   q u o t e s ,   a n d   c a n   c o n t a i n   t h e   f o l l o w i n g   C - l i k e   e s c a p e   
 
 s e q u e n c e s :   ' \ a '   ( b e l l ) ,   ' \ b '   ( b a c k s p a c e ) ,   ' \ f '   ( f o r m   f e e d ) ,   ' \ n '   ( n e w l i n e ) ,   ' \ r '   ( c a r r i a g e   r e t u r n ) ,   ' \ t '   ( h o r i z o n t a l   t a b ) ,   
 
 ' \ v '   ( v e r t i c a l   t a b ) ,   ' \ \ '   ( b a c k s l a s h ) ,   ' \ " '   ( q u o t a t i o n   m a r k   [ d o u b l e   q u o t e ] ) ,   a n d   ' \ ' '   ( a p o s t r o p h e   [ s i n g l e   q u o t e ] ) .   
 
 M o r e o v e r ,   a   b a c k s l a s h   f o l l o w e d   b y   a   r e a l   n e w l i n e   r e s u l t s   i n   a   n e w l i n e   i n   t h e   s t r i n g .   A   c h a r a c t e r   i n   a   s t r i n g   c a n   a l s o   b e   
 
 s p e c i f i e d   b y   i t s   n u m e r i c a l   v a l u e   u s i n g   t h e   e s c a p e   s e q u e n c e   \ d d d ,   w h e r e   d d d   i s   a   s e q u e n c e   o f   u p   t o   t h r e e   d e c i m a l   
 
 d i g i t s .   ( N o t e   t h a t   i f   a   n u m e r i c a l   e s c a p e   i s   t o   b e   f o l l o w e d   b y   a   d i g i t ,   i t   m u s t   b e   e x p r e s s e d   u s i n g   e x a c t l y   t h r e e   d i g i t s . )   
 
 S t r i n g s   i n   L u a   c a n   c o n t a i n   a n y   8 - b i t   v a l u e ,   i n c l u d i n g   e m b e d d e d   z e r o s ,   w h i c h   c a n   b e   s p e c i f i e d   a s   ' \ 0 ' .   
 
 
 
 L i t e r a l   s t r i n g s   c a n   a l s o   b e   d e f i n e d   u s i n g   a   l o n g   f o r m a t   e n c l o s e d   b y   l o n g   b r a c k e t s .   W e   d e f i n e   a n   o p e n i n g   l o n g   b r a c k e t   o f   
 
 l e v e l   n   a s   a n   o p e n i n g   s q u a r e   b r a c k e t   f o l l o w e d   b y   n   e q u a l   s i g n s   f o l l o w e d   b y   a n o t h e r   o p e n i n g   s q u a r e   b r a c k e t .   S o ,   a n   o p e n i n g   
 
 l o n g   b r a c k e t   o f   l e v e l   0   i s   w r i t t e n   a s   [ [ ,   a n   o p e n i n g   l o n g   b r a c k e t   o f   l e v e l   1   i s   w r i t t e n   a s   [ = [ ,   a n d   s o   o n .   A   c l o s i n g   l o n g   
 
 b r a c k e t   i s   d e f i n e d   s i m i l a r l y ;   f o r   i n s t a n c e ,   a   c l o s i n g   l o n g   b r a c k e t   o f   l e v e l   4   i s   w r i t t e n   a s   ] = = = = ] .   A   l o n g   s t r i n g   s t a r t s   
 
 w i t h   a n   o p e n i n g   l o n g   b r a c k e t   o f   a n y   l e v e l   a n d   e n d s   a t   t h e   f i r s t   c l o s i n g   l o n g   b r a c k e t   o f   t h e   s a m e   l e v e l .   L i t e r a l s   i n   t h i s   
 
 b r a c k e t e d   f o r m   c a n   r u n   f o r   s e v e r a l   l i n e s ,   d o   n o t   i n t e r p r e t   a n y   e s c a p e   s e q u e n c e s ,   a n d   i g n o r e   l o n g   b r a c k e t s   o f   a n y   o t h e r   
 
 l e v e l .   T h e y   c a n   c o n t a i n   a n y t h i n g   e x c e p t   a   c l o s i n g   b r a c k e t   o f   t h e   p r o p e r   l e v e l .   
 
 
 
 F o r   c o n v e n i e n c e ,   w h e n   t h e   o p e n i n g   l o n g   b r a c k e t   i s   i m m e d i a t e l y   f o l l o w e d   b y   a   n e w l i n e ,   t h e   n e w l i n e   i s   n o t   i n c l u d e d   i n   t h e   
 
 s t r i n g .   A s   a n   e x a m p l e ,   i n   a   s y s t e m   u s i n g   A S C I I   ( i n   w h i c h   ' a '   i s   c o d e d   a s   9 7 ,   n e w l i n e   i s   c o d e d   a s   1 0 ,   a n d   ' 1 '   i s   c o d e d   a s   4 9 ) ,   
 
 t h e   f i v e   l i t e r a l   s t r i n g s   b e l o w   d e n o t e   t h e   s a m e   s t r i n g :   
 
 
 
           a   =   ' a l o \ n 1 2 3 " ' 
 
           a   =   " a l o \ n 1 2 3 \ " " 
 
           a   =   ' \ 9 7 l o \ 1 0 \ 0 4 9 2 3 " ' 
 
           a   =   [ [ a l o 
 
           1 2 3 " ] ] 
 
           a   =   [ = = [ 
 
           a l o 
 
           1 2 3 " ] = = ] 
 
 
 
 A   n u m e r i c a l   c o n s t a n t   c a n   b e   w r i t t e n   w i t h   a n   o p t i o n a l   d e c i m a l   p a r t   a n d   a n   o p t i o n a l   d e c i m a l   e x p o n e n t .   L u a   a l s o   a c c e p t s   
 
 i n t e g e r   h e x a d e c i m a l   c o n s t a n t s ,   b y   p r e f i x i n g   t h e m   w i t h   0 x .   E x a m p l e s   o f   v a l i d   n u m e r i c a l   c o n s t a n t s   a r e   
 
 
 
           3       3 . 0       3 . 1 4 1 6       3 1 4 . 1 6 e - 2       0 . 3 1 4 1 6 E 1       0 x f f       0 x 5 6 
 
 
 
 A   c o m m e n t   s t a r t s   w i t h   a   d o u b l e   h y p h e n   ( - - )   a n y w h e r e   o u t s i d e   a   s t r i n g .   I f   t h e   t e x t   i m m e d i a t e l y   a f t e r   - -   i s   n o t   a n   o p e n i n g   
 
 l o n g   b r a c k e t ,   t h e   c o m m e n t   i s   a   s h o r t   c o m m e n t ,   w h i c h   r u n s   u n t i l   t h e   e n d   o f   t h e   l i n e .   O t h e r w i s e ,   i t   i s   a   l o n g   c o m m e n t ,   w h i c h   
 
 r u n s   u n t i l   t h e   c o r r e s p o n d i n g   c l o s i n g   l o n g   b r a c k e t .   L o n g   c o m m e n t s   a r e   f r e q u e n t l y   u s e d   t o   d i s a b l e   c o d e   t e m p o r a r i l y .   
 
 
 
 2 . 2   -   V a l u e s   a n d   T y p e s 
 
 L u a   i s   a   d y n a m i c a l l y   t y p e d   l a n g u a g e .   T h i s   m e a n s   t h a t   v a r i a b l e s   d o   n o t   h a v e   t y p e s ;   o n l y   v a l u e s   d o .   T h e r e   a r e   n o   t y p e   
 
 d e f i n i t i o n s   i n   t h e   l a n g u a g e .   A l l   v a l u e s   c a r r y   t h e i r   o w n   t y p e .   
 
 
 
 A l l   v a l u e s   i n   L u a   a r e   f i r s t - c l a s s   v a l u e s .   T h i s   m e a n s   t h a t   a l l   v a l u e s   c a n   b e   s t o r e d   i n   v a r i a b l e s ,   p a s s e d   a s   a r g u m e n t s   t o   o t h e r   
 
 f u n c t i o n s ,   a n d   r e t u r n e d   a s   r e s u l t s .   
 
 
 
 T h e r e   a r e   e i g h t   b a s i c   t y p e s   i n   L u a :   n i l ,   b o o l e a n ,   n u m b e r ,   s t r i n g ,   f u n c t i o n ,   u s e r d a t a ,   t h r e a d ,   a n d   t a b l e .   N i l   i s   t h e   t y p e   o f   
 
 t h e   v a l u e   n i l ,   w h o s e   m a i n   p r o p e r t y   i s   t o   b e   d i f f e r e n t   f r o m   a n y   o t h e r   v a l u e ;   i t   u s u a l l y   r e p r e s e n t s   t h e   a b s e n c e   o f   a   u s e f u l   
 
 v a l u e .   B o o l e a n   i s   t h e   t y p e   o f   t h e   v a l u e s   f a l s e   a n d   t r u e .   B o t h   n i l   a n d   f a l s e   m a k e   a   c o n d i t i o n   f a l s e ;   a n y   o t h e r   v a l u e   m a k e s   
 
 i t   t r u e .   N u m b e r   r e p r e s e n t s   r e a l   ( d o u b l e - p r e c i s i o n   f l o a t i n g - p o i n t )   n u m b e r s .   ( I t   i s   e a s y   t o   b u i l d   L u a   i n t e r p r e t e r s   t h a t   u s e   
 
 o t h e r   i n t e r n a l   r e p r e s e n t a t i o n s   f o r   n u m b e r s ,   s u c h   a s   s i n g l e - p r e c i s i o n   f l o a t   o r   l o n g   i n t e g e r s ;   s e e   f i l e   l u a c o n f . h . )   S t r i n g   
 
 r e p r e s e n t s   a r r a y s   o f   c h a r a c t e r s .   L u a   i s   8 - b i t   c l e a n :   s t r i n g s   c a n   c o n t a i n   a n y   8 - b i t   c h a r a c t e r ,   i n c l u d i n g   e m b e d d e d   z e r o s   ( ' \ 0 ' )   
 
 ( s e e    2 . 1 ) .   
 
 
 
 L u a   c a n   c a l l   ( a n d   m a n i p u l a t e )   f u n c t i o n s   w r i t t e n   i n   L u a   a n d   f u n c t i o n s   w r i t t e n   i n   C   ( s e e    2 . 5 . 8 ) .   
 
 
 
 T h e   t y p e   u s e r d a t a   i s   p r o v i d e d   t o   a l l o w   a r b i t r a r y   C   d a t a   t o   b e   s t o r e d   i n   L u a   v a r i a b l e s .   T h i s   t y p e   c o r r e s p o n d s   t o   a   b l o c k   o f   
 
 r a w   m e m o r y   a n d   h a s   n o   p r e - d e f i n e d   o p e r a t i o n s   i n   L u a ,   e x c e p t   a s s i g n m e n t   a n d   i d e n t i t y   t e s t .   H o w e v e r ,   b y   u s i n g   m e t a t a b l e s ,   t h e   
 
 p r o g r a m m e r   c a n   d e f i n e   o p e r a t i o n s   f o r   u s e r d a t a   v a l u e s   ( s e e    2 . 8 ) .   U s e r d a t a   v a l u e s   c a n n o t   b e   c r e a t e d   o r   m o d i f i e d   i n   L u a ,   o n l y   
 
 t h r o u g h   t h e   C   A P I .   T h i s   g u a r a n t e e s   t h e   i n t e g r i t y   o f   d a t a   o w n e d   b y   t h e   h o s t   p r o g r a m .   
 
 
 
 T h e   t y p e   t h r e a d   r e p r e s e n t s   i n d e p e n d e n t   t h r e a d s   o f   e x e c u t i o n   a n d   i t   i s   u s e d   t o   i m p l e m e n t   c o r o u t i n e s   ( s e e    2 . 1 1 ) .   D o   n o t   
 
 c o n f u s e   L u a   t h r e a d s   w i t h   o p e r a t i n g - s y s t e m   t h r e a d s .   L u a   s u p p o r t s   c o r o u t i n e s   o n   a l l   s y s t e m s ,   e v e n   t h o s e   t h a t   d o   n o t   s u p p o r t   
 
 t h r e a d s .   
 
 
 
 T h e   t y p e   t a b l e   i m p l e m e n t s   a s s o c i a t i v e   a r r a y s ,   t h a t   i s ,   a r r a y s   t h a t   c a n   b e   i n d e x e d   n o t   o n l y   w i t h   n u m b e r s ,   b u t   w i t h   a n y   v a l u e   
 
 ( e x c e p t   n i l ) .   T a b l e s   c a n   b e   h e t e r o g e n e o u s ;   t h a t   i s ,   t h e y   c a n   c o n t a i n   v a l u e s   o f   a l l   t y p e s   ( e x c e p t   n i l ) .   T a b l e s   a r e   t h e   s o l e   
 
 d a t a   s t r u c t u r i n g   m e c h a n i s m   i n   L u a ;   t h e y   c a n   b e   u s e d   t o   r e p r e s e n t   o r d i n a r y   a r r a y s ,   s y m b o l   t a b l e s ,   s e t s ,   r e c o r d s ,   g r a p h s ,   
 
 t r e e s ,   e t c .   T o   r e p r e s e n t   r e c o r d s ,   L u a   u s e s   t h e   f i e l d   n a m e   a s   a n   i n d e x .   T h e   l a n g u a g e   s u p p o r t s   t h i s   r e p r e s e n t a t i o n   b y   
 
 p r o v i d i n g   a . n a m e   a s   s y n t a c t i c   s u g a r   f o r   a [ " n a m e " ] .   T h e r e   a r e   s e v e r a l   c o n v e n i e n t   w a y s   t o   c r e a t e   t a b l e s   i n   L u a   ( s e e    2 . 5 . 7 ) .   
 
 
 
 L i k e   i n d i c e s ,   t h e   v a l u e   o f   a   t a b l e   f i e l d   c a n   b e   o f   a n y   t y p e   ( e x c e p t   n i l ) .   I n   p a r t i c u l a r ,   b e c a u s e   f u n c t i o n s   a r e   f i r s t - c l a s s   
 
 v a l u e s ,   t a b l e   f i e l d s   c a n   c o n t a i n   f u n c t i o n s .   T h u s   t a b l e s   c a n   a l s o   c a r r y   m e t h o d s   ( s e e    2 . 5 . 9 ) .   
 
 
 
 T a b l e s ,   f u n c t i o n s ,   t h r e a d s ,   a n d   ( f u l l )   u s e r d a t a   v a l u e s   a r e   o b j e c t s :   v a r i a b l e s   d o   n o t   a c t u a l l y   c o n t a i n   t h e s e   v a l u e s ,   o n l y   
 
 r e f e r e n c e s   t o   t h e m .   A s s i g n m e n t ,   p a r a m e t e r   p a s s i n g ,   a n d   f u n c t i o n   r e t u r n s   a l w a y s   m a n i p u l a t e   r e f e r e n c e s   t o   s u c h   v a l u e s ;   t h e s e   
 
 o p e r a t i o n s   d o   n o t   i m p l y   a n y   k i n d   o f   c o p y .   
 
 
 
 T h e   l i b r a r y   f u n c t i o n   t y p e   r e t u r n s   a   s t r i n g   d e s c r i b i n g   t h e   t y p e   o f   a   g i v e n   v a l u e .   
 
 
 
 2 . 2 . 1   -   C o e r c i o n 
 
 L u a   p r o v i d e s   a u t o m a t i c   c o n v e r s i o n   b e t w e e n   s t r i n g   a n d   n u m b e r   v a l u e s   a t   r u n   t i m e .   A n y   a r i t h m e t i c   o p e r a t i o n   a p p l i e d   t o   a   s t r i n g   
 
 t r i e s   t o   c o n v e r t   t h i s   s t r i n g   t o   a   n u m b e r ,   f o l l o w i n g   t h e   u s u a l   c o n v e r s i o n   r u l e s .   C o n v e r s e l y ,   w h e n e v e r   a   n u m b e r   i s   u s e d   w h e r e   
 
 a   s t r i n g   i s   e x p e c t e d ,   t h e   n u m b e r   i s   c o n v e r t e d   t o   a   s t r i n g ,   i n   a   r e a s o n a b l e   f o r m a t .   F o r   c o m p l e t e   c o n t r o l   o v e r   h o w   n u m b e r s   
 
 a r e   c o n v e r t e d   t o   s t r i n g s ,   u s e   t h e   f o r m a t   f u n c t i o n   f r o m   t h e   s t r i n g   l i b r a r y   ( s e e   s t r i n g . f o r m a t ) .   
 
 
 
 2 . 3   -   V a r i a b l e s 
 
 V a r i a b l e s   a r e   p l a c e s   t h a t   s t o r e   v a l u e s .   T h e r e   a r e   t h r e e   k i n d s   o f   v a r i a b l e s   i n   L u a :   g l o b a l   v a r i a b l e s ,   l o c a l   v a r i a b l e s ,   a n d   
 
 t a b l e   f i e l d s .   
 
 
 
 A   s i n g l e   n a m e   c a n   d e n o t e   a   g l o b a l   v a r i a b l e   o r   a   l o c a l   v a r i a b l e   ( o r   a   f u n c t i o n ' s   f o r m a l   p a r a m e t e r ,   w h i c h   i s   a   p a r t i c u l a r   
 
 k i n d   o f   l o c a l   v a r i a b l e ) :   
 
 
 
 	 v a r   : : =   N a m e 
 
 
 
 N a m e   d e n o t e s   i d e n t i f i e r s ,   a s   d e f i n e d   i n    2 . 1 .   
 
 
 
 A n y   v a r i a b l e   i s   a s s u m e d   t o   b e   g l o b a l   u n l e s s   e x p l i c i t l y   d e c l a r e d   a s   a   l o c a l   ( s e e    2 . 4 . 7 ) .   L o c a l   v a r i a b l e s   a r e   l e x i c a l l y   
 
 s c o p e d :   l o c a l   v a r i a b l e s   c a n   b e   f r e e l y   a c c e s s e d   b y   f u n c t i o n s   d e f i n e d   i n s i d e   t h e i r   s c o p e   ( s e e    2 . 6 ) .   
 
 
 
 B e f o r e   t h e   f i r s t   a s s i g n m e n t   t o   a   v a r i a b l e ,   i t s   v a l u e   i s   n i l .   
 
 
 
 S q u a r e   b r a c k e t s   a r e   u s e d   t o   i n d e x   a   t a b l e :   
 
 
 
 	 v a r   : : =   p r e f i x e x p   ` [    e x p   ` ]  
 
 
 
 T h e   m e a n i n g   o f   a c c e s s e s   t o   g l o b a l   v a r i a b l e s   a n d   t a b l e   f i e l d s   c a n   b e   c h a n g e d   v i a   m e t a t a b l e s .   A n   a c c e s s   t o   a n   i n d e x e d   
 
 v a r i a b l e   t [ i ]   i s   e q u i v a l e n t   t o   a   c a l l   g e t t a b l e _ e v e n t ( t , i ) .   ( S e e    2 . 8   f o r   a   c o m p l e t e   d e s c r i p t i o n   o f   t h e   g e t t a b l e _ e v e n t   
 
 f u n c t i o n .   T h i s   f u n c t i o n   i s   n o t   d e f i n e d   o r   c a l l a b l e   i n   L u a .   W e   u s e   i t   h e r e   o n l y   f o r   e x p l a n a t o r y   p u r p o s e s . )   
 
 
 
 T h e   s y n t a x   v a r . N a m e   i s   j u s t   s y n t a c t i c   s u g a r   f o r   v a r [ " N a m e " ] :   
 
 
 
 	 v a r   : : =   p r e f i x e x p   ` .    N a m e 
 
 
 
 A l l   g l o b a l   v a r i a b l e s   l i v e   a s   f i e l d s   i n   o r d i n a r y   L u a   t a b l e s ,   c a l l e d   e n v i r o n m e n t   t a b l e s   o r   s i m p l y   e n v i r o n m e n t s   ( s e e    2 . 9 ) .   
 
 E a c h   f u n c t i o n   h a s   i t s   o w n   r e f e r e n c e   t o   a n   e n v i r o n m e n t ,   s o   t h a t   a l l   g l o b a l   v a r i a b l e s   i n   t h i s   f u n c t i o n   w i l l   r e f e r   t o   t h i s   
 
 e n v i r o n m e n t   t a b l e .   W h e n   a   f u n c t i o n   i s   c r e a t e d ,   i t   i n h e r i t s   t h e   e n v i r o n m e n t   f r o m   t h e   f u n c t i o n   t h a t   c r e a t e d   i t .   T o   g e t   t h e   
 
 e n v i r o n m e n t   t a b l e   o f   a   L u a   f u n c t i o n ,   y o u   c a l l   g e t f e n v .   T o   r e p l a c e   i t ,   y o u   c a l l   s e t f e n v .   ( Y o u   c a n   o n l y   m a n i p u l a t e   t h e   
 
 e n v i r o n m e n t   o f   C   f u n c t i o n s   t h r o u g h   t h e   d e b u g   l i b r a r y ;   ( s e e    5 . 9 ) . )   
 
 
 
 A n   a c c e s s   t o   a   g l o b a l   v a r i a b l e   x   i s   e q u i v a l e n t   t o   _ e n v . x ,   w h i c h   i n   t u r n   i s   e q u i v a l e n t   t o   
 
 
 
           g e t t a b l e _ e v e n t ( _ e n v ,   " x " ) 
 
 
 
 w h e r e   _ e n v   i s   t h e   e n v i r o n m e n t   o f   t h e   r u n n i n g   f u n c t i o n .   ( S e e    2 . 8   f o r   a   c o m p l e t e   d e s c r i p t i o n   o f   t h e   g e t t a b l e _ e v e n t   f u n c t i o n .   
 
 T h i s   f u n c t i o n   i s   n o t   d e f i n e d   o r   c a l l a b l e   i n   L u a .   S i m i l a r l y ,   t h e   _ e n v   v a r i a b l e   i s   n o t   d e f i n e d   i n   L u a .   W e   u s e   t h e m   h e r e   o n l y   
 
 f o r   e x p l a n a t o r y   p u r p o s e s . )   
 
 
 
 2 . 4   -   S t a t e m e n t s 
 
 L u a   s u p p o r t s   a n   a l m o s t   c o n v e n t i o n a l   s e t   o f   s t a t e m e n t s ,   s i m i l a r   t o   t h o s e   i n   P a s c a l   o r   C .   T h i s   s e t   i n c l u d e s   a s s i g n m e n t s ,   
 
 c o n t r o l   s t r u c t u r e s ,   f u n c t i o n   c a l l s ,   a n d   v a r i a b l e   d e c l a r a t i o n s .   
 
 
 
 2 . 4 . 1   -   C h u n k s 
 
 T h e   u n i t   o f   e x e c u t i o n   o f   L u a   i s   c a l l e d   a   c h u n k .   A   c h u n k   i s   s i m p l y   a   s e q u e n c e   o f   s t a t e m e n t s ,   w h i c h   a r e   e x e c u t e d   s e q u e n t i a l l y .   
 
 E a c h   s t a t e m e n t   c a n   b e   o p t i o n a l l y   f o l l o w e d   b y   a   s e m i c o l o n :   
 
 
 
 	 c h u n k   : : =   { s t a t   [ ` ;  ] } 
 
 
 
 T h e r e   a r e   n o   e m p t y   s t a t e m e n t s   a n d   t h u s   ' ; ; '   i s   n o t   l e g a l .   
 
 
 
 L u a   h a n d l e s   a   c h u n k   a s   t h e   b o d y   o f   a n   a n o n y m o u s   f u n c t i o n   w i t h   a   v a r i a b l e   n u m b e r   o f   a r g u m e n t s   ( s e e    2 . 5 . 9 ) .   A s   s u c h ,   c h u n k s   
 
 c a n   d e f i n e   l o c a l   v a r i a b l e s ,   r e c e i v e   a r g u m e n t s ,   a n d   r e t u r n   v a l u e s .   
 
 
 
 A   c h u n k   c a n   b e   s t o r e d   i n   a   f i l e   o r   i n   a   s t r i n g   i n s i d e   t h e   h o s t   p r o g r a m .   T o   e x e c u t e   a   c h u n k ,   L u a   f i r s t   p r e - c o m p i l e s   t h e   
 
 c h u n k   i n t o   i n s t r u c t i o n s   f o r   a   v i r t u a l   m a c h i n e ,   a n d   t h e n   i t   e x e c u t e s   t h e   c o m p i l e d   c o d e   w i t h   a n   i n t e r p r e t e r   f o r   t h e   v i r t u a l   
 
 m a c h i n e .   
 
 
 
 C h u n k s   c a n   a l s o   b e   p r e - c o m p i l e d   i n t o   b i n a r y   f o r m ;   s e e   p r o g r a m   l u a c   f o r   d e t a i l s .   P r o g r a m s   i n   s o u r c e   a n d   c o m p i l e d   f o r m s   a r e   
 
 i n t e r c h a n g e a b l e ;   L u a   a u t o m a t i c a l l y   d e t e c t s   t h e   f i l e   t y p e   a n d   a c t s   a c c o r d i n g l y .   
 
 
 
 2 . 4 . 2   -   B l o c k s 
 
 A   b l o c k   i s   a   l i s t   o f   s t a t e m e n t s ;   s y n t a c t i c a l l y ,   a   b l o c k   i s   t h e   s a m e   a s   a   c h u n k :   
 
 
 
 	 b l o c k   : : =   c h u n k 
 
 
 
 A   b l o c k   c a n   b e   e x p l i c i t l y   d e l i m i t e d   t o   p r o d u c e   a   s i n g l e   s t a t e m e n t :   
 
 
 
 	 s t a t   : : =   d o   b l o c k   e n d 
 
 
 
 E x p l i c i t   b l o c k s   a r e   u s e f u l   t o   c o n t r o l   t h e   s c o p e   o f   v a r i a b l e   d e c l a r a t i o n s .   E x p l i c i t   b l o c k s   a r e   a l s o   s o m e t i m e s   u s e d   t o   a d d   a   
 
 r e t u r n   o r   b r e a k   s t a t e m e n t   i n   t h e   m i d d l e   o f   a n o t h e r   b l o c k   ( s e e    2 . 4 . 4 ) .   
 
 
 
 2 . 4 . 3   -   A s s i g n m e n t 
 
 L u a   a l l o w s   m u l t i p l e   a s s i g n m e n t s .   T h e r e f o r e ,   t h e   s y n t a x   f o r   a s s i g n m e n t   d e f i n e s   a   l i s t   o f   v a r i a b l e s   o n   t h e   l e f t   s i d e   a n d   a   
 
 l i s t   o f   e x p r e s s i o n s   o n   t h e   r i g h t   s i d e .   T h e   e l e m e n t s   i n   b o t h   l i s t s   a r e   s e p a r a t e d   b y   c o m m a s :   
 
 
 
 	 s t a t   : : =   v a r l i s t   ` =    e x p l i s t 
 
 	 v a r l i s t   : : =   v a r   { ` ,    v a r } 
 
 	 e x p l i s t   : : =   e x p   { ` ,    e x p } 
 
 
 
 E x p r e s s i o n s   a r e   d i s c u s s e d   i n    2 . 5 .   
 
 
 
 B e f o r e   t h e   a s s i g n m e n t ,   t h e   l i s t   o f   v a l u e s   i s   a d j u s t e d   t o   t h e   l e n g t h   o f   t h e   l i s t   o f   v a r i a b l e s .   I f   t h e r e   a r e   m o r e   v a l u e s   t h a n   
 
 n e e d e d ,   t h e   e x c e s s   v a l u e s   a r e   t h r o w n   a w a y .   I f   t h e r e   a r e   f e w e r   v a l u e s   t h a n   n e e d e d ,   t h e   l i s t   i s   e x t e n d e d   w i t h   a s   m a n y   n i l ' s   
 
 a s   n e e d e d .   I f   t h e   l i s t   o f   e x p r e s s i o n s   e n d s   w i t h   a   f u n c t i o n   c a l l ,   t h e n   a l l   v a l u e s   r e t u r n e d   b y   t h a t   c a l l   e n t e r   t h e   l i s t   o f   
 
 v a l u e s ,   b e f o r e   t h e   a d j u s t m e n t   ( e x c e p t   w h e n   t h e   c a l l   i s   e n c l o s e d   i n   p a r e n t h e s e s ;   s e e    2 . 5 ) .   
 
 
 
 T h e   a s s i g n m e n t   s t a t e m e n t   f i r s t   e v a l u a t e s   a l l   i t s   e x p r e s s i o n s   a n d   o n l y   t h e n   a r e   t h e   a s s i g n m e n t s   p e r f o r m e d .   T h u s   t h e   c o d e   
 
 
 
           i   =   3 
 
           i ,   a [ i ]   =   i + 1 ,   2 0 
 
 
 
 s e t s   a [ 3 ]   t o   2 0 ,   w i t h o u t   a f f e c t i n g   a [ 4 ]   b e c a u s e   t h e   i   i n   a [ i ]   i s   e v a l u a t e d   ( t o   3 )   b e f o r e   i t   i s   a s s i g n e d   4 .   S i m i l a r l y ,   t h e   
 
 l i n e   
 
 
 
           x ,   y   =   y ,   x 
 
 
 
 e x c h a n g e s   t h e   v a l u e s   o f   x   a n d   y ,   a n d   
 
 
 
           x ,   y ,   z   =   y ,   z ,   x 
 
 
 
 c y c l i c a l l y   p e r m u t e s   t h e   v a l u e s   o f   x ,   y ,   a n d   z .   
 
 
 
 T h e   m e a n i n g   o f   a s s i g n m e n t s   t o   g l o b a l   v a r i a b l e s   a n d   t a b l e   f i e l d s   c a n   b e   c h a n g e d   v i a   m e t a t a b l e s .   A n   a s s i g n m e n t   t o   a n   i n d e x e d   
 
 v a r i a b l e   t [ i ]   =   v a l   i s   e q u i v a l e n t   t o   s e t t a b l e _ e v e n t ( t , i , v a l ) .   ( S e e    2 . 8   f o r   a   c o m p l e t e   d e s c r i p t i o n   o f   t h e   s e t t a b l e _ e v e n t   
 
 f u n c t i o n .   T h i s   f u n c t i o n   i s   n o t   d e f i n e d   o r   c a l l a b l e   i n   L u a .   W e   u s e   i t   h e r e   o n l y   f o r   e x p l a n a t o r y   p u r p o s e s . )   
 
 
 
 A n   a s s i g n m e n t   t o   a   g l o b a l   v a r i a b l e   x   =   v a l   i s   e q u i v a l e n t   t o   t h e   a s s i g n m e n t   _ e n v . x   =   v a l ,   w h i c h   i n   t u r n   i s   e q u i v a l e n t   t o   
 
 
 
           s e t t a b l e _ e v e n t ( _ e n v ,   " x " ,   v a l ) 
 
 
 
 w h e r e   _ e n v   i s   t h e   e n v i r o n m e n t   o f   t h e   r u n n i n g   f u n c t i o n .   ( T h e   _ e n v   v a r i a b l e   i s   n o t   d e f i n e d   i n   L u a .   W e   u s e   i t   h e r e   o n l y   f o r   
 
 e x p l a n a t o r y   p u r p o s e s . )   
 
 
 
 2 . 4 . 4   -   C o n t r o l   S t r u c t u r e s 
 
 T h e   c o n t r o l   s t r u c t u r e s   i f ,   w h i l e ,   a n d   r e p e a t   h a v e   t h e   u s u a l   m e a n i n g   a n d   f a m i l i a r   s y n t a x :   
 
 
 
 	 s t a t   : : =   w h i l e   e x p   d o   b l o c k   e n d 
 
 	 s t a t   : : =   r e p e a t   b l o c k   u n t i l   e x p 
 
 	 s t a t   : : =   i f   e x p   t h e n   b l o c k   { e l s e i f   e x p   t h e n   b l o c k }   [ e l s e   b l o c k ]   e n d 
 
 
 
 L u a   a l s o   h a s   a   f o r   s t a t e m e n t ,   i n   t w o   f l a v o r s   ( s e e    2 . 4 . 5 ) .   
 
 
 
 T h e   c o n d i t i o n   e x p r e s s i o n   o f   a   c o n t r o l   s t r u c t u r e   c a n   r e t u r n   a n y   v a l u e .   B o t h   f a l s e   a n d   n i l   a r e   c o n s i d e r e d   f a l s e .   A l l   v a l u e s   
 
 d i f f e r e n t   f r o m   n i l   a n d   f a l s e   a r e   c o n s i d e r e d   t r u e   ( i n   p a r t i c u l a r ,   t h e   n u m b e r   0   a n d   t h e   e m p t y   s t r i n g   a r e   a l s o   t r u e ) .   
 
 
 
 I n   t h e   r e p e a t  u n t i l   l o o p ,   t h e   i n n e r   b l o c k   d o e s   n o t   e n d   a t   t h e   u n t i l   k e y w o r d ,   b u t   o n l y   a f t e r   t h e   c o n d i t i o n .   S o ,   t h e   
 
 c o n d i t i o n   c a n   r e f e r   t o   l o c a l   v a r i a b l e s   d e c l a r e d   i n s i d e   t h e   l o o p   b l o c k .   
 
 
 
 T h e   r e t u r n   s t a t e m e n t   i s   u s e d   t o   r e t u r n   v a l u e s   f r o m   a   f u n c t i o n   o r   a   c h u n k   ( w h i c h   i s   j u s t   a   f u n c t i o n ) .   F u n c t i o n s   a n d   c h u n k s   
 
 c a n   r e t u r n   m o r e   t h a n   o n e   v a l u e ,   a n d   s o   t h e   s y n t a x   f o r   t h e   r e t u r n   s t a t e m e n t   i s   
 
 
 
 	 s t a t   : : =   r e t u r n   [ e x p l i s t ] 
 
 
 
 T h e   b r e a k   s t a t e m e n t   i s   u s e d   t o   t e r m i n a t e   t h e   e x e c u t i o n   o f   a   w h i l e ,   r e p e a t ,   o r   f o r   l o o p ,   s k i p p i n g   t o   t h e   n e x t   s t a t e m e n t   
 
 a f t e r   t h e   l o o p :   
 
 
 
 	 s t a t   : : =   b r e a k 
 
 
 
 A   b r e a k   e n d s   t h e   i n n e r m o s t   e n c l o s i n g   l o o p .   
 
 
 
 T h e   r e t u r n   a n d   b r e a k   s t a t e m e n t s   c a n   o n l y   b e   w r i t t e n   a s   t h e   l a s t   s t a t e m e n t   o f   a   b l o c k .   I f   i t   i s   r e a l l y   n e c e s s a r y   t o   r e t u r n   
 
 o r   b r e a k   i n   t h e   m i d d l e   o f   a   b l o c k ,   t h e n   a n   e x p l i c i t   i n n e r   b l o c k   c a n   b e   u s e d ,   a s   i n   t h e   i d i o m s   d o   r e t u r n   e n d   a n d   d o   b r e a k   
 
 e n d ,   b e c a u s e   n o w   r e t u r n   a n d   b r e a k   a r e   t h e   l a s t   s t a t e m e n t s   i n   t h e i r   ( i n n e r )   b l o c k s .   
 
 
 
 2 . 4 . 5   -   F o r   S t a t e m e n t 
 
 T h e   f o r   s t a t e m e n t   h a s   t w o   f o r m s :   o n e   n u m e r i c   a n d   o n e   g e n e r i c .   
 
 
 
 T h e   n u m e r i c   f o r   l o o p   r e p e a t s   a   b l o c k   o f   c o d e   w h i l e   a   c o n t r o l   v a r i a b l e   r u n s   t h r o u g h   a n   a r i t h m e t i c   p r o g r e s s i o n .   I t   h a s   t h e   
 
 f o l l o w i n g   s y n t a x :   
 
 
 
 	 s t a t   : : =   f o r   N a m e   ` =    e x p   ` ,    e x p   [ ` ,    e x p ]   d o   b l o c k   e n d 
 
 
 
 T h e   b l o c k   i s   r e p e a t e d   f o r   n a m e   s t a r t i n g   a t   t h e   v a l u e   o f   t h e   f i r s t   e x p ,   u n t i l   i t   p a s s e s   t h e   s e c o n d   e x p   b y   s t e p s   o f   t h e   t h i r d   
 
 e x p .   M o r e   p r e c i s e l y ,   a   f o r   s t a t e m e n t   l i k e   
 
 
 
           f o r   v   =   e 1 ,   e 2 ,   e 3   d o   b l o c k   e n d 
 
 
 
 i s   e q u i v a l e n t   t o   t h e   c o d e :   
 
 
 
           d o 
 
               l o c a l   v a r ,   l i m i t ,   s t e p   =   t o n u m b e r ( e 1 ) ,   t o n u m b e r ( e 2 ) ,   t o n u m b e r ( e 3 ) 
 
               i f   n o t   ( v a r   a n d   l i m i t   a n d   s t e p )   t h e n   e r r o r ( )   e n d 
 
               w h i l e   ( s t e p   >   0   a n d   v a r   < =   l i m i t )   o r   ( s t e p   < =   0   a n d   v a r   > =   l i m i t )   d o 
 
                   l o c a l   v   =   v a r 
 
                   b l o c k 
 
                   v a r   =   v a r   +   s t e p 
 
               e n d 
 
           e n d 
 
 
 
 N o t e   t h e   f o l l o w i n g :   
 
 
 
 A l l   t h r e e   c o n t r o l   e x p r e s s i o n s   a r e   e v a l u a t e d   o n l y   o n c e ,   b e f o r e   t h e   l o o p   s t a r t s .   T h e y   m u s t   a l l   r e s u l t   i n   n u m b e r s .   
 
 v a r ,   l i m i t ,   a n d   s t e p   a r e   i n v i s i b l e   v a r i a b l e s .   T h e   n a m e s   s h o w n   h e r e   a r e   f o r   e x p l a n a t o r y   p u r p o s e s   o n l y .   
 
 I f   t h e   t h i r d   e x p r e s s i o n   ( t h e   s t e p )   i s   a b s e n t ,   t h e n   a   s t e p   o f   1   i s   u s e d .   
 
 Y o u   c a n   u s e   b r e a k   t o   e x i t   a   f o r   l o o p .   
 
 T h e   l o o p   v a r i a b l e   v   i s   l o c a l   t o   t h e   l o o p ;   y o u   c a n n o t   u s e   i t s   v a l u e   a f t e r   t h e   f o r   e n d s   o r   i s   b r o k e n .   I f   y o u   n e e d   t h i s   v a l u e ,   
 
 a s s i g n   i t   t o   a n o t h e r   v a r i a b l e   b e f o r e   b r e a k i n g   o r   e x i t i n g   t h e   l o o p .   
 
 T h e   g e n e r i c   f o r   s t a t e m e n t   w o r k s   o v e r   f u n c t i o n s ,   c a l l e d   i t e r a t o r s .   O n   e a c h   i t e r a t i o n ,   t h e   i t e r a t o r   f u n c t i o n   i s   c a l l e d   t o   
 
 p r o d u c e   a   n e w   v a l u e ,   s t o p p i n g   w h e n   t h i s   n e w   v a l u e   i s   n i l .   T h e   g e n e r i c   f o r   l o o p   h a s   t h e   f o l l o w i n g   s y n t a x :   
 
 
 
 	 s t a t   : : =   f o r   n a m e l i s t   i n   e x p l i s t   d o   b l o c k   e n d 
 
 	 n a m e l i s t   : : =   N a m e   { ` ,    N a m e } 
 
 
 
 A   f o r   s t a t e m e n t   l i k e   
 
 
 
           f o r   v a r _ 1 ,      ,   v a r _ n   i n   e x p l i s t   d o   b l o c k   e n d 
 
 
 
 i s   e q u i v a l e n t   t o   t h e   c o d e :   
 
 
 
           d o 
 
               l o c a l   f ,   s ,   v a r   =   e x p l i s t 
 
               w h i l e   t r u e   d o 
 
                   l o c a l   v a r _ 1 ,      ,   v a r _ n   =   f ( s ,   v a r ) 
 
                   v a r   =   v a r _ 1 
 
                   i f   v a r   = =   n i l   t h e n   b r e a k   e n d 
 
                   b l o c k 
 
               e n d 
 
           e n d 
 
 
 
 N o t e   t h e   f o l l o w i n g :   
 
 
 
 e x p l i s t   i s   e v a l u a t e d   o n l y   o n c e .   I t s   r e s u l t s   a r e   a n   i t e r a t o r   f u n c t i o n ,   a   s t a t e ,   a n d   a n   i n i t i a l   v a l u e   f o r   t h e   f i r s t   i t e r a t o r   
 
 v a r i a b l e .   
 
 f ,   s ,   a n d   v a r   a r e   i n v i s i b l e   v a r i a b l e s .   T h e   n a m e s   a r e   h e r e   f o r   e x p l a n a t o r y   p u r p o s e s   o n l y .   
 
 Y o u   c a n   u s e   b r e a k   t o   e x i t   a   f o r   l o o p .   
 
 T h e   l o o p   v a r i a b l e s   v a r _ i   a r e   l o c a l   t o   t h e   l o o p ;   y o u   c a n n o t   u s e   t h e i r   v a l u e s   a f t e r   t h e   f o r   e n d s .   I f   y o u   n e e d   t h e s e   v a l u e s ,   
 
 t h e n   a s s i g n   t h e m   t o   o t h e r   v a r i a b l e s   b e f o r e   b r e a k i n g   o r   e x i t i n g   t h e   l o o p .   
 
 2 . 4 . 6   -   F u n c t i o n   C a l l s   a s   S t a t e m e n t s 
 
 T o   a l l o w   p o s s i b l e   s i d e - e f f e c t s ,   f u n c t i o n   c a l l s   c a n   b e   e x e c u t e d   a s   s t a t e m e n t s :   
 
 
 
 	 s t a t   : : =   f u n c t i o n c a l l 
 
 
 
 I n   t h i s   c a s e ,   a l l   r e t u r n e d   v a l u e s   a r e   t h r o w n   a w a y .   F u n c t i o n   c a l l s   a r e   e x p l a i n e d   i n    2 . 5 . 8 .   
 
 
 
 2 . 4 . 7   -   L o c a l   D e c l a r a t i o n s 
 
 L o c a l   v a r i a b l e s   c a n   b e   d e c l a r e d   a n y w h e r e   i n s i d e   a   b l o c k .   T h e   d e c l a r a t i o n   c a n   i n c l u d e   a n   i n i t i a l   a s s i g n m e n t :   
 
 
 
 	 s t a t   : : =   l o c a l   n a m e l i s t   [ ` =    e x p l i s t ] 
 
 
 
 I f   p r e s e n t ,   a n   i n i t i a l   a s s i g n m e n t   h a s   t h e   s a m e   s e m a n t i c s   o f   a   m u l t i p l e   a s s i g n m e n t   ( s e e    2 . 4 . 3 ) .   O t h e r w i s e ,   a l l   v a r i a b l e s   
 
 a r e   i n i t i a l i z e d   w i t h   n i l .   
 
 
 
 A   c h u n k   i s   a l s o   a   b l o c k   ( s e e    2 . 4 . 1 ) ,   a n d   s o   l o c a l   v a r i a b l e s   c a n   b e   d e c l a r e d   i n   a   c h u n k   o u t s i d e   a n y   e x p l i c i t   b l o c k .   T h e   
 
 s c o p e   o f   s u c h   l o c a l   v a r i a b l e s   e x t e n d s   u n t i l   t h e   e n d   o f   t h e   c h u n k .   
 
 
 
 T h e   v i s i b i l i t y   r u l e s   f o r   l o c a l   v a r i a b l e s   a r e   e x p l a i n e d   i n    2 . 6 .   
 
 
 
 2 . 5   -   E x p r e s s i o n s 
 
 T h e   b a s i c   e x p r e s s i o n s   i n   L u a   a r e   t h e   f o l l o w i n g :   
 
 
 
 	 e x p   : : =   p r e f i x e x p 
 
 	 e x p   : : =   n i l   |   f a l s e   |   t r u e 
 
 	 e x p   : : =   N u m b e r 
 
 	 e x p   : : =   S t r i n g 
 
 	 e x p   : : =   f u n c t i o n 
 
 	 e x p   : : =   t a b l e c o n s t r u c t o r 
 
 	 e x p   : : =   ` . . .  
 
 	 e x p   : : =   e x p   b i n o p   e x p 
 
 	 e x p   : : =   u n o p   e x p 
 
 	 p r e f i x e x p   : : =   v a r   |   f u n c t i o n c a l l   |   ` (    e x p   ` )  
 
 
 
 N u m b e r s   a n d   l i t e r a l   s t r i n g s   a r e   e x p l a i n e d   i n    2 . 1 ;   v a r i a b l e s   a r e   e x p l a i n e d   i n    2 . 3 ;   f u n c t i o n   d e f i n i t i o n s   a r e   e x p l a i n e d   i n   
 
  2 . 5 . 9 ;   f u n c t i o n   c a l l s   a r e   e x p l a i n e d   i n    2 . 5 . 8 ;   t a b l e   c o n s t r u c t o r s   a r e   e x p l a i n e d   i n    2 . 5 . 7 .   V a r a r g   e x p r e s s i o n s ,   d e n o t e d   b y   
 
 t h r e e   d o t s   ( ' . . . ' ) ,   c a n   o n l y   b e   u s e d   w h e n   d i r e c t l y   i n s i d e   a   v a r a r g   f u n c t i o n ;   t h e y   a r e   e x p l a i n e d   i n    2 . 5 . 9 .   
 
 
 
 B i n a r y   o p e r a t o r s   c o m p r i s e   a r i t h m e t i c   o p e r a t o r s   ( s e e    2 . 5 . 1 ) ,   r e l a t i o n a l   o p e r a t o r s   ( s e e    2 . 5 . 2 ) ,   l o g i c a l   o p e r a t o r s   
 
 ( s e e    2 . 5 . 3 ) ,   a n d   t h e   c o n c a t e n a t i o n   o p e r a t o r   ( s e e    2 . 5 . 4 ) .   U n a r y   o p e r a t o r s   c o m p r i s e   t h e   u n a r y   m i n u s   ( s e e    2 . 5 . 1 ) ,   t h e   u n a r y   
 
 n o t   ( s e e    2 . 5 . 3 ) ,   a n d   t h e   u n a r y   l e n g t h   o p e r a t o r   ( s e e    2 . 5 . 5 ) .   
 
 
 
 B o t h   f u n c t i o n   c a l l s   a n d   v a r a r g   e x p r e s s i o n s   c a n   r e s u l t   i n   m u l t i p l e   v a l u e s .   I f   a n   e x p r e s s i o n   i s   u s e d   a s   a   s t a t e m e n t   
 
 ( o n l y   p o s s i b l e   f o r   f u n c t i o n   c a l l s   ( s e e    2 . 4 . 6 ) ) ,   t h e n   i t s   r e t u r n   l i s t   i s   a d j u s t e d   t o   z e r o   e l e m e n t s ,   t h u s   d i s c a r d i n g   a l l   
 
 r e t u r n e d   v a l u e s .   I f   a n   e x p r e s s i o n   i s   u s e d   a s   t h e   l a s t   ( o r   t h e   o n l y )   e l e m e n t   o f   a   l i s t   o f   e x p r e s s i o n s ,   t h e n   n o   a d j u s t m e n t   i s   
 
 m a d e   ( u n l e s s   t h e   c a l l   i s   e n c l o s e d   i n   p a r e n t h e s e s ) .   I n   a l l   o t h e r   c o n t e x t s ,   L u a   a d j u s t s   t h e   r e s u l t   l i s t   t o   o n e   e l e m e n t ,   
 
 d i s c a r d i n g   a l l   v a l u e s   e x c e p t   t h e   f i r s t   o n e .   
 
 
 
 H e r e   a r e   s o m e   e x a m p l e s :   
 
 
 
           f ( )                                 - -   a d j u s t e d   t o   0   r e s u l t s 
 
           g ( f ( ) ,   x )                     - -   f ( )   i s   a d j u s t e d   t o   1   r e s u l t 
 
           g ( x ,   f ( ) )                     - -   g   g e t s   x   p l u s   a l l   r e s u l t s   f r o m   f ( ) 
 
           a , b , c   =   f ( ) ,   x           - -   f ( )   i s   a d j u s t e d   t o   1   r e s u l t   ( c   g e t s   n i l ) 
 
           a , b   =   . . .                     - -   a   g e t s   t h e   f i r s t   v a r a r g   p a r a m e t e r ,   b   g e t s 
 
                                                 - -   t h e   s e c o n d   ( b o t h   a   a n d   b   c a n   g e t   n i l   i f   t h e r e 
 
                                                 - -   i s   n o   c o r r e s p o n d i n g   v a r a r g   p a r a m e t e r ) 
 
           
 
           a , b , c   =   x ,   f ( )           - -   f ( )   i s   a d j u s t e d   t o   2   r e s u l t s 
 
           a , b , c   =   f ( )                 - -   f ( )   i s   a d j u s t e d   t o   3   r e s u l t s 
 
           r e t u r n   f ( )                   - -   r e t u r n s   a l l   r e s u l t s   f r o m   f ( ) 
 
           r e t u r n   . . .                   - -   r e t u r n s   a l l   r e c e i v e d   v a r a r g   p a r a m e t e r s 
 
           r e t u r n   x , y , f ( )           - -   r e t u r n s   x ,   y ,   a n d   a l l   r e s u l t s   f r o m   f ( ) 
 
           { f ( ) }                             - -   c r e a t e s   a   l i s t   w i t h   a l l   r e s u l t s   f r o m   f ( ) 
 
           { . . . }                             - -   c r e a t e s   a   l i s t   w i t h   a l l   v a r a r g   p a r a m e t e r s 
 
           { f ( ) ,   n i l }                   - -   f ( )   i s   a d j u s t e d   t o   1   r e s u l t 
 
 
 
 A n y   e x p r e s s i o n   e n c l o s e d   i n   p a r e n t h e s e s   a l w a y s   r e s u l t s   i n   o n l y   o n e   v a l u e .   T h u s ,   ( f ( x , y , z ) )   i s   a l w a y s   a   s i n g l e   v a l u e ,   e v e n   i f   
 
 f   r e t u r n s   s e v e r a l   v a l u e s .   ( T h e   v a l u e   o f   ( f ( x , y , z ) )   i s   t h e   f i r s t   v a l u e   r e t u r n e d   b y   f   o r   n i l   i f   f   d o e s   n o t   r e t u r n   a n y   v a l u e s . )   
 
 
 
 2 . 5 . 1   -   A r i t h m e t i c   O p e r a t o r s 
 
 L u a   s u p p o r t s   t h e   u s u a l   a r i t h m e t i c   o p e r a t o r s :   t h e   b i n a r y   +   ( a d d i t i o n ) ,   -   ( s u b t r a c t i o n ) ,   *   ( m u l t i p l i c a t i o n ) ,   /   ( d i v i s i o n ) ,   
 
 %   ( m o d u l o ) ,   a n d   ^   ( e x p o n e n t i a t i o n ) ;   a n d   u n a r y   -   ( n e g a t i o n ) .   I f   t h e   o p e r a n d s   a r e   n u m b e r s ,   o r   s t r i n g s   t h a t   c a n   b e   c o n v e r t e d   
 
 t o   n u m b e r s   ( s e e    2 . 2 . 1 ) ,   t h e n   a l l   o p e r a t i o n s   h a v e   t h e   u s u a l   m e a n i n g .   E x p o n e n t i a t i o n   w o r k s   f o r   a n y   e x p o n e n t .   F o r   i n s t a n c e ,   
 
 x ^ ( - 0 . 5 )   c o m p u t e s   t h e   i n v e r s e   o f   t h e   s q u a r e   r o o t   o f   x .   M o d u l o   i s   d e f i n e d   a s   
 
 
 
           a   %   b   = =   a   -   m a t h . f l o o r ( a / b ) * b 
 
 
 
 T h a t   i s ,   i t   i s   t h e   r e m a i n d e r   o f   a   d i v i s i o n   t h a t   r o u n d s   t h e   q u o t i e n t   t o w a r d s   m i n u s   i n f i n i t y .   
 
 
 
 2 . 5 . 2   -   R e l a t i o n a l   O p e r a t o r s 
 
 T h e   r e l a t i o n a l   o p e r a t o r s   i n   L u a   a r e   
 
 
 
           = =         ~ =         <           >           < =         > = 
 
 
 
 T h e s e   o p e r a t o r s   a l w a y s   r e s u l t   i n   f a l s e   o r   t r u e .   
 
 
 
 E q u a l i t y   ( = = )   f i r s t   c o m p a r e s   t h e   t y p e   o f   i t s   o p e r a n d s .   I f   t h e   t y p e s   a r e   d i f f e r e n t ,   t h e n   t h e   r e s u l t   i s   f a l s e .   O t h e r w i s e ,   t h e   
 
 v a l u e s   o f   t h e   o p e r a n d s   a r e   c o m p a r e d .   N u m b e r s   a n d   s t r i n g s   a r e   c o m p a r e d   i n   t h e   u s u a l   w a y .   O b j e c t s   ( t a b l e s ,   u s e r d a t a ,   t h r e a d s ,   
 
 a n d   f u n c t i o n s )   a r e   c o m p a r e d   b y   r e f e r e n c e :   t w o   o b j e c t s   a r e   c o n s i d e r e d   e q u a l   o n l y   i f   t h e y   a r e   t h e   s a m e   o b j e c t .   E v e r y   t i m e   y o u   
 
 c r e a t e   a   n e w   o b j e c t   ( a   t a b l e ,   u s e r d a t a ,   t h r e a d ,   o r   f u n c t i o n ) ,   t h i s   n e w   o b j e c t   i s   d i f f e r e n t   f r o m   a n y   p r e v i o u s l y   e x i s t i n g   
 
 o b j e c t .   
 
 
 
 Y o u   c a n   c h a n g e   t h e   w a y   t h a t   L u a   c o m p a r e s   t a b l e s   a n d   u s e r d a t a   b y   u s i n g   t h e   " e q "   m e t a m e t h o d   ( s e e    2 . 8 ) .   
 
 
 
 T h e   c o n v e r s i o n   r u l e s   o f    2 . 2 . 1   d o   n o t   a p p l y   t o   e q u a l i t y   c o m p a r i s o n s .   T h u s ,   " 0 " = = 0   e v a l u a t e s   t o   f a l s e ,   a n d   t [ 0 ]   a n d   t [ " 0 " ]   
 
 d e n o t e   d i f f e r e n t   e n t r i e s   i n   a   t a b l e .   
 
 
 
 T h e   o p e r a t o r   ~ =   i s   e x a c t l y   t h e   n e g a t i o n   o f   e q u a l i t y   ( = = ) .   
 
 
 
 T h e   o r d e r   o p e r a t o r s   w o r k   a s   f o l l o w s .   I f   b o t h   a r g u m e n t s   a r e   n u m b e r s ,   t h e n   t h e y   a r e   c o m p a r e d   a s   s u c h .   O t h e r w i s e ,   i f   b o t h   
 
 a r g u m e n t s   a r e   s t r i n g s ,   t h e n   t h e i r   v a l u e s   a r e   c o m p a r e d   a c c o r d i n g   t o   t h e   c u r r e n t   l o c a l e .   O t h e r w i s e ,   L u a   t r i e s   t o   c a l l   t h e   
 
 " l t "   o r   t h e   " l e "   m e t a m e t h o d   ( s e e    2 . 8 ) .   A   c o m p a r i s o n   a   >   b   i s   t r a n s l a t e d   t o   b   <   a   a n d   a   > =   b   i s   t r a n s l a t e d   t o   b   < =   a .   
 
 
 
 2 . 5 . 3   -   L o g i c a l   O p e r a t o r s 
 
 T h e   l o g i c a l   o p e r a t o r s   i n   L u a   a r e   a n d ,   o r ,   a n d   n o t .   L i k e   t h e   c o n t r o l   s t r u c t u r e s   ( s e e    2 . 4 . 4 ) ,   a l l   l o g i c a l   o p e r a t o r s   c o n s i d e r   
 
 b o t h   f a l s e   a n d   n i l   a s   f a l s e   a n d   a n y t h i n g   e l s e   a s   t r u e .   
 
 
 
 T h e   n e g a t i o n   o p e r a t o r   n o t   a l w a y s   r e t u r n s   f a l s e   o r   t r u e .   T h e   c o n j u n c t i o n   o p e r a t o r   a n d   r e t u r n s   i t s   f i r s t   a r g u m e n t   i f   t h i s   
 
 v a l u e   i s   f a l s e   o r   n i l ;   o t h e r w i s e ,   a n d   r e t u r n s   i t s   s e c o n d   a r g u m e n t .   T h e   d i s j u n c t i o n   o p e r a t o r   o r   r e t u r n s   i t s   f i r s t   a r g u m e n t   
 
 i f   t h i s   v a l u e   i s   d i f f e r e n t   f r o m   n i l   a n d   f a l s e ;   o t h e r w i s e ,   o r   r e t u r n s   i t s   s e c o n d   a r g u m e n t .   B o t h   a n d   a n d   o r   u s e   s h o r t - c u t   
 
 e v a l u a t i o n ;   t h a t   i s ,   t h e   s e c o n d   o p e r a n d   i s   e v a l u a t e d   o n l y   i f   n e c e s s a r y .   H e r e   a r e   s o m e   e x a m p l e s :   
 
 
 
           1 0   o r   2 0                         - - >   1 0 
 
           1 0   o r   e r r o r ( )               - - >   1 0 
 
           n i l   o r   " a "                     - - >   " a " 
 
           n i l   a n d   1 0                     - - >   n i l 
 
           f a l s e   a n d   e r r o r ( )       - - >   f a l s e 
 
           f a l s e   a n d   n i l               - - >   f a l s e 
 
           f a l s e   o r   n i l                 - - >   n i l 
 
           1 0   a n d   2 0                       - - >   2 0 
 
 
 
 ( I n   t h i s   m a n u a l ,   - - >   i n d i c a t e s   t h e   r e s u l t   o f   t h e   p r e c e d i n g   e x p r e s s i o n . )   
 
 
 
 2 . 5 . 4   -   C o n c a t e n a t i o n 
 
 T h e   s t r i n g   c o n c a t e n a t i o n   o p e r a t o r   i n   L u a   i s   d e n o t e d   b y   t w o   d o t s   ( ' . . ' ) .   I f   b o t h   o p e r a n d s   a r e   s t r i n g s   o r   n u m b e r s ,   t h e n   t h e y   
 
 a r e   c o n v e r t e d   t o   s t r i n g s   a c c o r d i n g   t o   t h e   r u l e s   m e n t i o n e d   i n    2 . 2 . 1 .   O t h e r w i s e ,   t h e   " c o n c a t "   m e t a m e t h o d   i s   c a l l e d   ( s e e    2 . 8 ) .   
 
 
 
 2 . 5 . 5   -   T h e   L e n g t h   O p e r a t o r 
 
 T h e   l e n g t h   o p e r a t o r   i s   d e n o t e d   b y   t h e   u n a r y   o p e r a t o r   # .   T h e   l e n g t h   o f   a   s t r i n g   i s   i t s   n u m b e r   o f   b y t e s   ( t h a t   i s ,   t h e   u s u a l   
 
 m e a n i n g   o f   s t r i n g   l e n g t h   w h e n   e a c h   c h a r a c t e r   i s   o n e   b y t e ) .   
 
 
 
 T h e   l e n g t h   o f   a   t a b l e   t   i s   d e f i n e d   t o   b e   a n y   i n t e g e r   i n d e x   n   s u c h   t h a t   t [ n ]   i s   n o t   n i l   a n d   t [ n + 1 ]   i s   n i l ;   m o r e o v e r ,   
 
 i f   t [ 1 ]   i s   n i l ,   n   c a n   b e   z e r o .   F o r   a   r e g u l a r   a r r a y ,   w i t h   n o n - n i l   v a l u e s   f r o m   1   t o   a   g i v e n   n ,   i t s   l e n g t h   i s   e x a c t l y   t h a t   n ,   
 
 t h e   i n d e x   o f   i t s   l a s t   v a l u e .   I f   t h e   a r r a y   h a s   " h o l e s "   ( t h a t   i s ,   n i l   v a l u e s   b e t w e e n   o t h e r   n o n - n i l   v a l u e s ) ,   t h e n   # t   c a n   b e   
 
 a n y   o f   t h e   i n d i c e s   t h a t   d i r e c t l y   p r e c e d e s   a   n i l   v a l u e   ( t h a t   i s ,   i t   m a y   c o n s i d e r   a n y   s u c h   n i l   v a l u e   a s   t h e   e n d   o f   t h e   a r r a y ) .   
 
 
 
 2 . 5 . 6   -   P r e c e d e n c e 
 
 O p e r a t o r   p r e c e d e n c e   i n   L u a   f o l l o w s   t h e   t a b l e   b e l o w ,   f r o m   l o w e r   t o   h i g h e r   p r i o r i t y :   
 
 
 
           o r 
 
           a n d 
 
           <           >           < =         > =         ~ =         = = 
 
           . . 
 
           +           - 
 
           *           /           % 
 
           n o t       #           -   ( u n a r y ) 
 
           ^ 
 
 
 
 A s   u s u a l ,   y o u   c a n   u s e   p a r e n t h e s e s   t o   c h a n g e   t h e   p r e c e d e n c e s   o f   a n   e x p r e s s i o n .   T h e   c o n c a t e n a t i o n   ( ' . . ' )   a n d   e x p o n e n t i a t i o n   
 
 ( ' ^ ' )   o p e r a t o r s   a r e   r i g h t   a s s o c i a t i v e .   A l l   o t h e r   b i n a r y   o p e r a t o r s   a r e   l e f t   a s s o c i a t i v e .   
 
 
 
 2 . 5 . 7   -   T a b l e   C o n s t r u c t o r s 
 
 T a b l e   c o n s t r u c t o r s   a r e   e x p r e s s i o n s   t h a t   c r e a t e   t a b l e s .   E v e r y   t i m e   a   c o n s t r u c t o r   i s   e v a l u a t e d ,   a   n e w   t a b l e   i s   c r e a t e d .   A   
 
 c o n s t r u c t o r   c a n   b e   u s e d   t o   c r e a t e   a n   e m p t y   t a b l e   o r   t o   c r e a t e   a   t a b l e   a n d   i n i t i a l i z e   s o m e   o f   i t s   f i e l d s .   T h e   g e n e r a l   s y n t a x   
 
 f o r   c o n s t r u c t o r s   i s   
 
 
 
 	 t a b l e c o n s t r u c t o r   : : =   ` {    [ f i e l d l i s t ]   ` }  
 
 	 f i e l d l i s t   : : =   f i e l d   { f i e l d s e p   f i e l d }   [ f i e l d s e p ] 
 
 	 f i e l d   : : =   ` [    e x p   ` ]    ` =    e x p   |   N a m e   ` =    e x p   |   e x p 
 
 	 f i e l d s e p   : : =   ` ,    |   ` ;  
 
 
 
 E a c h   f i e l d   o f   t h e   f o r m   [ e x p 1 ]   =   e x p 2   a d d s   t o   t h e   n e w   t a b l e   a n   e n t r y   w i t h   k e y   e x p 1   a n d   v a l u e   e x p 2 .   A   f i e l d   o f   t h e   f o r m   
 
 n a m e   =   e x p   i s   e q u i v a l e n t   t o   [ " n a m e " ]   =   e x p .   F i n a l l y ,   f i e l d s   o f   t h e   f o r m   e x p   a r e   e q u i v a l e n t   t o   [ i ]   =   e x p ,   w h e r e   i   a r e   
 
 c o n s e c u t i v e   n u m e r i c a l   i n t e g e r s ,   s t a r t i n g   w i t h   1 .   F i e l d s   i n   t h e   o t h e r   f o r m a t s   d o   n o t   a f f e c t   t h i s   c o u n t i n g .   F o r   e x a m p l e ,   
 
 
 
           a   =   {   [ f ( 1 ) ]   =   g ;   " x " ,   " y " ;   x   =   1 ,   f ( x ) ,   [ 3 0 ]   =   2 3 ;   4 5   } 
 
 
 
 i s   e q u i v a l e n t   t o   
 
 
 
           d o 
 
               l o c a l   t   =   { } 
 
               t [ f ( 1 ) ]   =   g 
 
               t [ 1 ]   =   " x "                   - -   1 s t   e x p 
 
               t [ 2 ]   =   " y "                   - -   2 n d   e x p 
 
               t . x   =   1                         - -   t [ " x " ]   =   1 
 
               t [ 3 ]   =   f ( x )                 - -   3 r d   e x p 
 
               t [ 3 0 ]   =   2 3 
 
               t [ 4 ]   =   4 5                     - -   4 t h   e x p 
 
               a   =   t 
 
           e n d 
 
 
 
 I f   t h e   l a s t   f i e l d   i n   t h e   l i s t   h a s   t h e   f o r m   e x p   a n d   t h e   e x p r e s s i o n   i s   a   f u n c t i o n   c a l l   o r   a   v a r a r g   e x p r e s s i o n ,   t h e n   a l l   
 
 v a l u e s   r e t u r n e d   b y   t h i s   e x p r e s s i o n   e n t e r   t h e   l i s t   c o n s e c u t i v e l y   ( s e e    2 . 5 . 8 ) .   T o   a v o i d   t h i s ,   e n c l o s e   t h e   f u n c t i o n   c a l l   o r   
 
 t h e   v a r a r g   e x p r e s s i o n   i n   p a r e n t h e s e s   ( s e e    2 . 5 ) .   
 
 
 
 T h e   f i e l d   l i s t   c a n   h a v e   a n   o p t i o n a l   t r a i l i n g   s e p a r a t o r ,   a s   a   c o n v e n i e n c e   f o r   m a c h i n e - g e n e r a t e d   c o d e .   
 
 
 
 2 . 5 . 8   -   F u n c t i o n   C a l l s 
 
 A   f u n c t i o n   c a l l   i n   L u a   h a s   t h e   f o l l o w i n g   s y n t a x :   
 
 
 
 	 f u n c t i o n c a l l   : : =   p r e f i x e x p   a r g s 
 
 
 
 I n   a   f u n c t i o n   c a l l ,   f i r s t   p r e f i x e x p   a n d   a r g s   a r e   e v a l u a t e d .   I f   t h e   v a l u e   o f   p r e f i x e x p   h a s   t y p e   f u n c t i o n ,   t h e n   t h i s   f u n c t i o n   
 
 i s   c a l l e d   w i t h   t h e   g i v e n   a r g u m e n t s .   O t h e r w i s e ,   t h e   p r e f i x e x p   " c a l l "   m e t a m e t h o d   i s   c a l l e d ,   h a v i n g   a s   f i r s t   p a r a m e t e r   t h e   
 
 v a l u e   o f   p r e f i x e x p ,   f o l l o w e d   b y   t h e   o r i g i n a l   c a l l   a r g u m e n t s   ( s e e    2 . 8 ) .   
 
 
 
 T h e   f o r m   
 
 
 
 	 f u n c t i o n c a l l   : : =   p r e f i x e x p   ` :    N a m e   a r g s 
 
 
 
 c a n   b e   u s e d   t o   c a l l   " m e t h o d s " .   A   c a l l   v : n a m e ( a r g s )   i s   s y n t a c t i c   s u g a r   f o r   v . n a m e ( v , a r g s ) ,   e x c e p t   t h a t   v   i s   e v a l u a t e d   o n l y   
 
 o n c e .   
 
 
 
 A r g u m e n t s   h a v e   t h e   f o l l o w i n g   s y n t a x :   
 
 
 
 	 a r g s   : : =   ` (    [ e x p l i s t ]   ` )  
 
 	 a r g s   : : =   t a b l e c o n s t r u c t o r 
 
 	 a r g s   : : =   S t r i n g 
 
 
 
 A l l   a r g u m e n t   e x p r e s s i o n s   a r e   e v a l u a t e d   b e f o r e   t h e   c a l l .   A   c a l l   o f   t h e   f o r m   f { f i e l d s }   i s   s y n t a c t i c   s u g a r   f o r   f ( { f i e l d s } ) ;   
 
 t h a t   i s ,   t h e   a r g u m e n t   l i s t   i s   a   s i n g l e   n e w   t a b l e .   A   c a l l   o f   t h e   f o r m   f ' s t r i n g '   ( o r   f " s t r i n g "   o r   f [ [ s t r i n g ] ] )   i s   s y n t a c t i c   
 
 s u g a r   f o r   f ( ' s t r i n g ' ) ;   t h a t   i s ,   t h e   a r g u m e n t   l i s t   i s   a   s i n g l e   l i t e r a l   s t r i n g .   
 
 
 
 A s   a n   e x c e p t i o n   t o   t h e   f r e e - f o r m a t   s y n t a x   o f   L u a ,   y o u   c a n n o t   p u t   a   l i n e   b r e a k   b e f o r e   t h e   ' ( '   i n   a   f u n c t i o n   c a l l .   T h i s   
 
 r e s t r i c t i o n   a v o i d s   s o m e   a m b i g u i t i e s   i n   t h e   l a n g u a g e .   I f   y o u   w r i t e   
 
 
 
           a   =   f 
 
           ( g ) . x ( a ) 
 
 
 
 L u a   w o u l d   s e e   t h a t   a s   a   s i n g l e   s t a t e m e n t ,   a   =   f ( g ) . x ( a ) .   S o ,   i f   y o u   w a n t   t w o   s t a t e m e n t s ,   y o u   m u s t   a d d   a   s e m i - c o l o n   b e t w e e n   
 
 t h e m .   I f   y o u   a c t u a l l y   w a n t   t o   c a l l   f ,   y o u   m u s t   r e m o v e   t h e   l i n e   b r e a k   b e f o r e   ( g ) .   
 
 
 
 A   c a l l   o f   t h e   f o r m   r e t u r n   f u n c t i o n c a l l   i s   c a l l e d   a   t a i l   c a l l .   L u a   i m p l e m e n t s   p r o p e r   t a i l   c a l l s   ( o r   p r o p e r   t a i l   r e c u r s i o n ) :   
 
 i n   a   t a i l   c a l l ,   t h e   c a l l e d   f u n c t i o n   r e u s e s   t h e   s t a c k   e n t r y   o f   t h e   c a l l i n g   f u n c t i o n .   T h e r e f o r e ,   t h e r e   i s   n o   l i m i t   o n   t h e   
 
 n u m b e r   o f   n e s t e d   t a i l   c a l l s   t h a t   a   p r o g r a m   c a n   e x e c u t e .   H o w e v e r ,   a   t a i l   c a l l   e r a s e s   a n y   d e b u g   i n f o r m a t i o n   a b o u t   t h e   c a l l i n g   
 
 f u n c t i o n .   N o t e   t h a t   a   t a i l   c a l l   o n l y   h a p p e n s   w i t h   a   p a r t i c u l a r   s y n t a x ,   w h e r e   t h e   r e t u r n   h a s   o n e   s i n g l e   f u n c t i o n   c a l l   a s   
 
 a r g u m e n t ;   t h i s   s y n t a x   m a k e s   t h e   c a l l i n g   f u n c t i o n   r e t u r n   e x a c t l y   t h e   r e t u r n s   o f   t h e   c a l l e d   f u n c t i o n .   S o ,   n o n e   o f   t h e   
 
 f o l l o w i n g   e x a m p l e s   a r e   t a i l   c a l l s :   
 
 
 
           r e t u r n   ( f ( x ) )                 - -   r e s u l t s   a d j u s t e d   t o   1 
 
           r e t u r n   2   *   f ( x ) 
 
           r e t u r n   x ,   f ( x )               - -   a d d i t i o n a l   r e s u l t s 
 
           f ( x ) ;   r e t u r n                   - -   r e s u l t s   d i s c a r d e d 
 
           r e t u r n   x   o r   f ( x )           - -   r e s u l t s   a d j u s t e d   t o   1 
 
 
 
 2 . 5 . 9   -   F u n c t i o n   D e f i n i t i o n s 
 
 T h e   s y n t a x   f o r   f u n c t i o n   d e f i n i t i o n   i s   
 
 
 
 	 f u n c t i o n   : : =   f u n c t i o n   f u n c b o d y 
 
 	 f u n c b o d y   : : =   ` (    [ p a r l i s t ]   ` )    b l o c k   e n d 
 
 
 
 T h e   f o l l o w i n g   s y n t a c t i c   s u g a r   s i m p l i f i e s   f u n c t i o n   d e f i n i t i o n s :   
 
 
 
 	 s t a t   : : =   f u n c t i o n   f u n c n a m e   f u n c b o d y 
 
 	 s t a t   : : =   l o c a l   f u n c t i o n   N a m e   f u n c b o d y 
 
 	 f u n c n a m e   : : =   N a m e   { ` .    N a m e }   [ ` :    N a m e ] 
 
 
 
 T h e   s t a t e m e n t   
 
 
 
           f u n c t i o n   f   ( )   b o d y   e n d 
 
 
 
 t r a n s l a t e s   t o   
 
 
 
           f   =   f u n c t i o n   ( )   b o d y   e n d 
 
 
 
 T h e   s t a t e m e n t   
 
 
 
           f u n c t i o n   t . a . b . c . f   ( )   b o d y   e n d 
 
 
 
 t r a n s l a t e s   t o   
 
 
 
           t . a . b . c . f   =   f u n c t i o n   ( )   b o d y   e n d 
 
 
 
 T h e   s t a t e m e n t   
 
 
 
           l o c a l   f u n c t i o n   f   ( )   b o d y   e n d 
 
 
 
 t r a n s l a t e s   t o   
 
 
 
           l o c a l   f ;   f   =   f u n c t i o n   ( )   b o d y   e n d 
 
 
 
 n o t   t o   
 
 
 
           l o c a l   f   =   f u n c t i o n   ( )   b o d y   e n d 
 
 
 
 ( T h i s   o n l y   m a k e s   a   d i f f e r e n c e   w h e n   t h e   b o d y   o f   t h e   f u n c t i o n   c o n t a i n s   r e f e r e n c e s   t o   f . )   
 
 
 
 A   f u n c t i o n   d e f i n i t i o n   i s   a n   e x e c u t a b l e   e x p r e s s i o n ,   w h o s e   v a l u e   h a s   t y p e   f u n c t i o n .   W h e n   L u a   p r e - c o m p i l e s   a   c h u n k ,   a l l   i t s   
 
 f u n c t i o n   b o d i e s   a r e   p r e - c o m p i l e d   t o o .   T h e n ,   w h e n e v e r   L u a   e x e c u t e s   t h e   f u n c t i o n   d e f i n i t i o n ,   t h e   f u n c t i o n   i s   i n s t a n t i a t e d   
 
 ( o r   c l o s e d ) .   T h i s   f u n c t i o n   i n s t a n c e   ( o r   c l o s u r e )   i s   t h e   f i n a l   v a l u e   o f   t h e   e x p r e s s i o n .   D i f f e r e n t   i n s t a n c e s   o f   t h e   s a m e   
 
 f u n c t i o n   c a n   r e f e r   t o   d i f f e r e n t   e x t e r n a l   l o c a l   v a r i a b l e s   a n d   c a n   h a v e   d i f f e r e n t   e n v i r o n m e n t   t a b l e s .   
 
 
 
 P a r a m e t e r s   a c t   a s   l o c a l   v a r i a b l e s   t h a t   a r e   i n i t i a l i z e d   w i t h   t h e   a r g u m e n t   v a l u e s :   
 
 
 
 	 p a r l i s t   : : =   n a m e l i s t   [ ` ,    ` . . .  ]   |   ` . . .  
 
 
 
 W h e n   a   f u n c t i o n   i s   c a l l e d ,   t h e   l i s t   o f   a r g u m e n t s   i s   a d j u s t e d   t o   t h e   l e n g t h   o f   t h e   l i s t   o f   p a r a m e t e r s ,   u n l e s s   t h e   f u n c t i o n   
 
 i s   a   v a r i a d i c   o r   v a r a r g   f u n c t i o n ,   w h i c h   i s   i n d i c a t e d   b y   t h r e e   d o t s   ( ' . . . ' )   a t   t h e   e n d   o f   i t s   p a r a m e t e r   l i s t .   A   v a r a r g   
 
 f u n c t i o n   d o e s   n o t   a d j u s t   i t s   a r g u m e n t   l i s t ;   i n s t e a d ,   i t   c o l l e c t s   a l l   e x t r a   a r g u m e n t s   a n d   s u p p l i e s   t h e m   t o   t h e   f u n c t i o n   
 
 t h r o u g h   a   v a r a r g   e x p r e s s i o n ,   w h i c h   i s   a l s o   w r i t t e n   a s   t h r e e   d o t s .   T h e   v a l u e   o f   t h i s   e x p r e s s i o n   i s   a   l i s t   o f   a l l   a c t u a l   
 
 e x t r a   a r g u m e n t s ,   s i m i l a r   t o   a   f u n c t i o n   w i t h   m u l t i p l e   r e s u l t s .   I f   a   v a r a r g   e x p r e s s i o n   i s   u s e d   i n s i d e   a n o t h e r   e x p r e s s i o n   o r   
 
 i n   t h e   m i d d l e   o f   a   l i s t   o f   e x p r e s s i o n s ,   t h e n   i t s   r e t u r n   l i s t   i s   a d j u s t e d   t o   o n e   e l e m e n t .   I f   t h e   e x p r e s s i o n   i s   u s e d   a s   t h e   
 
 l a s t   e l e m e n t   o f   a   l i s t   o f   e x p r e s s i o n s ,   t h e n   n o   a d j u s t m e n t   i s   m a d e   ( u n l e s s   t h a t   l a s t   e x p r e s s i o n   i s   e n c l o s e d   i n   p a r e n t h e s e s ) .   
 
 
 
 A s   a n   e x a m p l e ,   c o n s i d e r   t h e   f o l l o w i n g   d e f i n i t i o n s :   
 
 
 
           f u n c t i o n   f ( a ,   b )   e n d 
 
           f u n c t i o n   g ( a ,   b ,   . . . )   e n d 
 
           f u n c t i o n   r ( )   r e t u r n   1 , 2 , 3   e n d 
 
 
 
 T h e n ,   w e   h a v e   t h e   f o l l o w i n g   m a p p i n g   f r o m   a r g u m e n t s   t o   p a r a m e t e r s   a n d   t o   t h e   v a r a r g   e x p r e s s i o n :   
 
 
 
           C A L L                         P A R A M E T E R S 
 
           
 
           f ( 3 )                           a = 3 ,   b = n i l 
 
           f ( 3 ,   4 )                     a = 3 ,   b = 4 
 
           f ( 3 ,   4 ,   5 )               a = 3 ,   b = 4 
 
           f ( r ( ) ,   1 0 )               a = 1 ,   b = 1 0 
 
           f ( r ( ) )                       a = 1 ,   b = 2 
 
           
 
           g ( 3 )                           a = 3 ,   b = n i l ,   . . .   - - >     ( n o t h i n g ) 
 
           g ( 3 ,   4 )                     a = 3 ,   b = 4 ,       . . .   - - >     ( n o t h i n g ) 
 
           g ( 3 ,   4 ,   5 ,   8 )         a = 3 ,   b = 4 ,       . . .   - - >     5     8 
 
           g ( 5 ,   r ( ) )                 a = 5 ,   b = 1 ,       . . .   - - >     2     3 
 
 
 
 R e s u l t s   a r e   r e t u r n e d   u s i n g   t h e   r e t u r n   s t a t e m e n t   ( s e e    2 . 4 . 4 ) .   I f   c o n t r o l   r e a c h e s   t h e   e n d   o f   a   f u n c t i o n   w i t h o u t   e n c o u n t e r i n g   
 
 a   r e t u r n   s t a t e m e n t ,   t h e n   t h e   f u n c t i o n   r e t u r n s   w i t h   n o   r e s u l t s .   
 
 
 
 T h e   c o l o n   s y n t a x   i s   u s e d   f o r   d e f i n i n g   m e t h o d s ,   t h a t   i s ,   f u n c t i o n s   t h a t   h a v e   a n   i m p l i c i t   e x t r a   p a r a m e t e r   s e l f .   T h u s ,   t h e   
 
 s t a t e m e n t   
 
 
 
           f u n c t i o n   t . a . b . c : f   ( p a r a m s )   b o d y   e n d 
 
 
 
 i s   s y n t a c t i c   s u g a r   f o r   
 
 
 
           t . a . b . c . f   =   f u n c t i o n   ( s e l f ,   p a r a m s )   b o d y   e n d 
 
 
 
 2 . 6   -   V i s i b i l i t y   R u l e s 
 
 L u a   i s   a   l e x i c a l l y   s c o p e d   l a n g u a g e .   T h e   s c o p e   o f   v a r i a b l e s   b e g i n s   a t   t h e   f i r s t   s t a t e m e n t   a f t e r   t h e i r   d e c l a r a t i o n   a n d   l a s t s   
 
 u n t i l   t h e   e n d   o f   t h e   i n n e r m o s t   b l o c k   t h a t   i n c l u d e s   t h e   d e c l a r a t i o n .   C o n s i d e r   t h e   f o l l o w i n g   e x a m p l e :   
 
 
 
           x   =   1 0                                 - -   g l o b a l   v a r i a b l e 
 
           d o                                         - -   n e w   b l o c k 
 
               l o c a l   x   =   x                   - -   n e w   ' x ' ,   w i t h   v a l u e   1 0 
 
               p r i n t ( x )                         - - >   1 0 
 
               x   =   x + 1 
 
               d o                                     - -   a n o t h e r   b l o c k 
 
                   l o c a l   x   =   x + 1           - -   a n o t h e r   ' x ' 
 
                   p r i n t ( x )                     - - >   1 2 
 
               e n d 
 
               p r i n t ( x )                         - - >   1 1 
 
           e n d 
 
           p r i n t ( x )                             - - >   1 0     ( t h e   g l o b a l   o n e ) 
 
 
 
 N o t i c e   t h a t ,   i n   a   d e c l a r a t i o n   l i k e   l o c a l   x   =   x ,   t h e   n e w   x   b e i n g   d e c l a r e d   i s   n o t   i n   s c o p e   y e t ,   a n d   s o   t h e   s e c o n d   x   r e f e r s   t o   
 
 t h e   o u t s i d e   v a r i a b l e .   
 
 
 
 B e c a u s e   o f   t h e   l e x i c a l   s c o p i n g   r u l e s ,   l o c a l   v a r i a b l e s   c a n   b e   f r e e l y   a c c e s s e d   b y   f u n c t i o n s   d e f i n e d   i n s i d e   t h e i r   s c o p e .   A   
 
 l o c a l   v a r i a b l e   u s e d   b y   a n   i n n e r   f u n c t i o n   i s   c a l l e d   a n   u p v a l u e ,   o r   e x t e r n a l   l o c a l   v a r i a b l e ,   i n s i d e   t h e   i n n e r   f u n c t i o n .   
 
 
 
 N o t i c e   t h a t   e a c h   e x e c u t i o n   o f   a   l o c a l   s t a t e m e n t   d e f i n e s   n e w   l o c a l   v a r i a b l e s .   C o n s i d e r   t h e   f o l l o w i n g   e x a m p l e :   
 
 
 
           a   =   { } 
 
           l o c a l   x   =   2 0 
 
           f o r   i = 1 , 1 0   d o 
 
               l o c a l   y   =   0 
 
               a [ i ]   =   f u n c t i o n   ( )   y = y + 1 ;   r e t u r n   x + y   e n d 
 
           e n d 
 
 
 
 T h e   l o o p   c r e a t e s   t e n   c l o s u r e s   ( t h a t   i s ,   t e n   i n s t a n c e s   o f   t h e   a n o n y m o u s   f u n c t i o n ) .   E a c h   o f   t h e s e   c l o s u r e s   u s e s   a   d i f f e r e n t   
 
 y   v a r i a b l e ,   w h i l e   a l l   o f   t h e m   s h a r e   t h e   s a m e   x .   
 
 
 
 2 . 7   -   E r r o r   H a n d l i n g 
 
 B e c a u s e   L u a   i s   a n   e m b e d d e d   e x t e n s i o n   l a n g u a g e ,   a l l   L u a   a c t i o n s   s t a r t   f r o m   C   c o d e   i n   t h e   h o s t   p r o g r a m   c a l l i n g   a   f u n c t i o n   
 
 f r o m   t h e   L u a   l i b r a r y   ( s e e   l u a _ p c a l l ) .   W h e n e v e r   a n   e r r o r   o c c u r s   d u r i n g   L u a   c o m p i l a t i o n   o r   e x e c u t i o n ,   c o n t r o l   r e t u r n s   t o   C ,   
 
 w h i c h   c a n   t a k e   a p p r o p r i a t e   m e a s u r e s   ( s u c h   a s   p r i n t i n g   a n   e r r o r   m e s s a g e ) .   
 
 
 
 L u a   c o d e   c a n   e x p l i c i t l y   g e n e r a t e   a n   e r r o r   b y   c a l l i n g   t h e   e r r o r   f u n c t i o n .   I f   y o u   n e e d   t o   c a t c h   e r r o r s   i n   L u a ,   y o u   c a n   u s e   
 
 t h e   p c a l l   f u n c t i o n .   
 
 
 
 2 . 8   -   M e t a t a b l e s 
 
 E v e r y   v a l u e   i n   L u a   c a n   h a v e   a   m e t a t a b l e .   T h i s   m e t a t a b l e   i s   a n   o r d i n a r y   L u a   t a b l e   t h a t   d e f i n e s   t h e   b e h a v i o r   o f   t h e   o r i g i n a l   
 
 v a l u e   u n d e r   c e r t a i n   s p e c i a l   o p e r a t i o n s .   Y o u   c a n   c h a n g e   s e v e r a l   a s p e c t s   o f   t h e   b e h a v i o r   o f   o p e r a t i o n s   o v e r   a   v a l u e   b y   s e t t i n g   
 
 s p e c i f i c   f i e l d s   i n   i t s   m e t a t a b l e .   F o r   i n s t a n c e ,   w h e n   a   n o n - n u m e r i c   v a l u e   i s   t h e   o p e r a n d   o f   a n   a d d i t i o n ,   L u a   c h e c k s   f o r   a   
 
 f u n c t i o n   i n   t h e   f i e l d   " _ _ a d d "   i n   i t s   m e t a t a b l e .   I f   i t   f i n d s   o n e ,   L u a   c a l l s   t h i s   f u n c t i o n   t o   p e r f o r m   t h e   a d d i t i o n .   
 
 
 
 W e   c a l l   t h e   k e y s   i n   a   m e t a t a b l e   e v e n t s   a n d   t h e   v a l u e s   m e t a m e t h o d s .   I n   t h e   p r e v i o u s   e x a m p l e ,   t h e   e v e n t   i s   " a d d "   a n d   t h e   
 
 m e t a m e t h o d   i s   t h e   f u n c t i o n   t h a t   p e r f o r m s   t h e   a d d i t i o n .   
 
 
 
 Y o u   c a n   q u e r y   t h e   m e t a t a b l e   o f   a n y   v a l u e   t h r o u g h   t h e   g e t m e t a t a b l e   f u n c t i o n .   
 
 
 
 Y o u   c a n   r e p l a c e   t h e   m e t a t a b l e   o f   t a b l e s   t h r o u g h   t h e   s e t m e t a t a b l e   f u n c t i o n .   Y o u   c a n n o t   c h a n g e   t h e   m e t a t a b l e   o f   o t h e r   t y p e s   
 
 f r o m   L u a   ( e x c e p t   b y   u s i n g   t h e   d e b u g   l i b r a r y ) ;   y o u   m u s t   u s e   t h e   C   A P I   f o r   t h a t .   
 
 
 
 T a b l e s   a n d   f u l l   u s e r d a t a   h a v e   i n d i v i d u a l   m e t a t a b l e s   ( a l t h o u g h   m u l t i p l e   t a b l e s   a n d   u s e r d a t a   c a n   s h a r e   t h e i r   m e t a t a b l e s ) .   
 
 V a l u e s   o f   a l l   o t h e r   t y p e s   s h a r e   o n e   s i n g l e   m e t a t a b l e   p e r   t y p e ;   t h a t   i s ,   t h e r e   i s   o n e   s i n g l e   m e t a t a b l e   f o r   a l l   n u m b e r s ,   o n e   
 
 f o r   a l l   s t r i n g s ,   e t c .   
 
 
 
 A   m e t a t a b l e   c o n t r o l s   h o w   a n   o b j e c t   b e h a v e s   i n   a r i t h m e t i c   o p e r a t i o n s ,   o r d e r   c o m p a r i s o n s ,   c o n c a t e n a t i o n ,   l e n g t h   o p e r a t i o n ,   
 
 a n d   i n d e x i n g .   A   m e t a t a b l e   a l s o   c a n   d e f i n e   a   f u n c t i o n   t o   b e   c a l l e d   w h e n   a   u s e r d a t a   i s   g a r b a g e   c o l l e c t e d .   F o r   e a c h   o f   t h e s e   
 
 o p e r a t i o n s   L u a   a s s o c i a t e s   a   s p e c i f i c   k e y   c a l l e d   a n   e v e n t .   W h e n   L u a   p e r f o r m s   o n e   o f   t h e s e   o p e r a t i o n s   o v e r   a   v a l u e ,   i t   c h e c k s   
 
 w h e t h e r   t h i s   v a l u e   h a s   a   m e t a t a b l e   w i t h   t h e   c o r r e s p o n d i n g   e v e n t .   I f   s o ,   t h e   v a l u e   a s s o c i a t e d   w i t h   t h a t   k e y   ( t h e   m e t a m e t h o d )   
 
 c o n t r o l s   h o w   L u a   w i l l   p e r f o r m   t h e   o p e r a t i o n .   
 
 
 
 M e t a t a b l e s   c o n t r o l   t h e   o p e r a t i o n s   l i s t e d   n e x t .   E a c h   o p e r a t i o n   i s   i d e n t i f i e d   b y   i t s   c o r r e s p o n d i n g   n a m e .   T h e   k e y   f o r   e a c h   
 
 o p e r a t i o n   i s   a   s t r i n g   w i t h   i t s   n a m e   p r e f i x e d   b y   t w o   u n d e r s c o r e s ,   ' _ _ ' ;   f o r   i n s t a n c e ,   t h e   k e y   f o r   o p e r a t i o n   " a d d "   i s   t h e   
 
 s t r i n g   " _ _ a d d " .   T h e   s e m a n t i c s   o f   t h e s e   o p e r a t i o n s   i s   b e t t e r   e x p l a i n e d   b y   a   L u a   f u n c t i o n   d e s c r i b i n g   h o w   t h e   i n t e r p r e t e r   
 
 e x e c u t e s   t h e   o p e r a t i o n .   
 
 
 
 T h e   c o d e   s h o w n   h e r e   i n   L u a   i s   o n l y   i l l u s t r a t i v e ;   t h e   r e a l   b e h a v i o r   i s   h a r d   c o d e d   i n   t h e   i n t e r p r e t e r   a n d   i t   i s   m u c h   m o r e   
 
 e f f i c i e n t   t h a n   t h i s   s i m u l a t i o n .   A l l   f u n c t i o n s   u s e d   i n   t h e s e   d e s c r i p t i o n s   ( r a w g e t ,   t o n u m b e r ,   e t c . )   a r e   d e s c r i b e d   i n    5 . 1 .   I n   
 
 p a r t i c u l a r ,   t o   r e t r i e v e   t h e   m e t a m e t h o d   o f   a   g i v e n   o b j e c t ,   w e   u s e   t h e   e x p r e s s i o n   
 
 
 
           m e t a t a b l e ( o b j ) [ e v e n t ] 
 
 
 
 T h i s   s h o u l d   b e   r e a d   a s   
 
 
 
           r a w g e t ( g e t m e t a t a b l e ( o b j )   o r   { } ,   e v e n t ) 
 
 
 
 T h a t   i s ,   t h e   a c c e s s   t o   a   m e t a m e t h o d   d o e s   n o t   i n v o k e   o t h e r   m e t a m e t h o d s ,   a n d   t h e   a c c e s s   t o   o b j e c t s   w i t h   n o   m e t a t a b l e s   d o e s   
 
 n o t   f a i l   ( i t   s i m p l y   r e s u l t s   i n   n i l ) .   
 
 
 
 " a d d " :   t h e   +   o p e r a t i o n .   
 
 T h e   f u n c t i o n   g e t b i n h a n d l e r   b e l o w   d e f i n e s   h o w   L u a   c h o o s e s   a   h a n d l e r   f o r   a   b i n a r y   o p e r a t i o n .   F i r s t ,   L u a   t r i e s   t h e   f i r s t   
 
 o p e r a n d .   I f   i t s   t y p e   d o e s   n o t   d e f i n e   a   h a n d l e r   f o r   t h e   o p e r a t i o n ,   t h e n   L u a   t r i e s   t h e   s e c o n d   o p e r a n d .   
 
 
 
           f u n c t i o n   g e t b i n h a n d l e r   ( o p 1 ,   o p 2 ,   e v e n t ) 
 
               r e t u r n   m e t a t a b l e ( o p 1 ) [ e v e n t ]   o r   m e t a t a b l e ( o p 2 ) [ e v e n t ] 
 
           e n d 
 
 
 
 B y   u s i n g   t h i s   f u n c t i o n ,   t h e   b e h a v i o r   o f   t h e   o p 1   +   o p 2   i s   
 
 
 
           f u n c t i o n   a d d _ e v e n t   ( o p 1 ,   o p 2 ) 
 
               l o c a l   o 1 ,   o 2   =   t o n u m b e r ( o p 1 ) ,   t o n u m b e r ( o p 2 ) 
 
               i f   o 1   a n d   o 2   t h e n     - -   b o t h   o p e r a n d s   a r e   n u m e r i c ? 
 
                   r e t u r n   o 1   +   o 2       - -   ' + '   h e r e   i s   t h e   p r i m i t i v e   ' a d d ' 
 
               e l s e     - -   a t   l e a s t   o n e   o f   t h e   o p e r a n d s   i s   n o t   n u m e r i c 
 
                   l o c a l   h   =   g e t b i n h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ a d d " ) 
 
                   i f   h   t h e n 
 
                       - -   c a l l   t h e   h a n d l e r   w i t h   b o t h   o p e r a n d s 
 
                       r e t u r n   ( h ( o p 1 ,   o p 2 ) ) 
 
                   e l s e     - -   n o   h a n d l e r   a v a i l a b l e :   d e f a u l t   b e h a v i o r 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 
 
 " s u b " :   t h e   -   o p e r a t i o n .   B e h a v i o r   s i m i l a r   t o   t h e   " a d d "   o p e r a t i o n .   
 
 " m u l " :   t h e   *   o p e r a t i o n .   B e h a v i o r   s i m i l a r   t o   t h e   " a d d "   o p e r a t i o n .   
 
 " d i v " :   t h e   /   o p e r a t i o n .   B e h a v i o r   s i m i l a r   t o   t h e   " a d d "   o p e r a t i o n .   
 
 " m o d " :   t h e   %   o p e r a t i o n .   B e h a v i o r   s i m i l a r   t o   t h e   " a d d "   o p e r a t i o n ,   w i t h   t h e   o p e r a t i o n   o 1   -   f l o o r ( o 1 / o 2 ) * o 2   a s   t h e   p r i m i t i v e   
 
 o p e r a t i o n .   
 
 " p o w " :   t h e   ^   ( e x p o n e n t i a t i o n )   o p e r a t i o n .   B e h a v i o r   s i m i l a r   t o   t h e   " a d d "   o p e r a t i o n ,   w i t h   t h e   f u n c t i o n   p o w   ( f r o m   t h e   C   m a t h   
 
 l i b r a r y )   a s   t h e   p r i m i t i v e   o p e r a t i o n .   
 
 " u n m " :   t h e   u n a r y   -   o p e r a t i o n .   
 
           f u n c t i o n   u n m _ e v e n t   ( o p ) 
 
               l o c a l   o   =   t o n u m b e r ( o p ) 
 
               i f   o   t h e n     - -   o p e r a n d   i s   n u m e r i c ? 
 
                   r e t u r n   - o     - -   ' - '   h e r e   i s   t h e   p r i m i t i v e   ' u n m ' 
 
               e l s e     - -   t h e   o p e r a n d   i s   n o t   n u m e r i c . 
 
                   - -   T r y   t o   g e t   a   h a n d l e r   f r o m   t h e   o p e r a n d 
 
                   l o c a l   h   =   m e t a t a b l e ( o p ) . _ _ u n m 
 
                   i f   h   t h e n 
 
                       - -   c a l l   t h e   h a n d l e r   w i t h   t h e   o p e r a n d 
 
                       r e t u r n   ( h ( o p ) ) 
 
                   e l s e     - -   n o   h a n d l e r   a v a i l a b l e :   d e f a u l t   b e h a v i o r 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 
 
 " c o n c a t " :   t h e   . .   ( c o n c a t e n a t i o n )   o p e r a t i o n .   
 
           f u n c t i o n   c o n c a t _ e v e n t   ( o p 1 ,   o p 2 ) 
 
               i f   ( t y p e ( o p 1 )   = =   " s t r i n g "   o r   t y p e ( o p 1 )   = =   " n u m b e r " )   a n d 
 
                     ( t y p e ( o p 2 )   = =   " s t r i n g "   o r   t y p e ( o p 2 )   = =   " n u m b e r " )   t h e n 
 
                   r e t u r n   o p 1   . .   o p 2     - -   p r i m i t i v e   s t r i n g   c o n c a t e n a t i o n 
 
               e l s e 
 
                   l o c a l   h   =   g e t b i n h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ c o n c a t " ) 
 
                   i f   h   t h e n 
 
                       r e t u r n   ( h ( o p 1 ,   o p 2 ) ) 
 
                   e l s e 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 
 
 " l e n " :   t h e   #   o p e r a t i o n .   
 
           f u n c t i o n   l e n _ e v e n t   ( o p ) 
 
               i f   t y p e ( o p )   = =   " s t r i n g "   t h e n 
 
                   r e t u r n   s t r l e n ( o p )                   - -   p r i m i t i v e   s t r i n g   l e n g t h 
 
               e l s e i f   t y p e ( o p )   = =   " t a b l e "   t h e n 
 
                   r e t u r n   # o p                                 - -   p r i m i t i v e   t a b l e   l e n g t h 
 
               e l s e 
 
                   l o c a l   h   =   m e t a t a b l e ( o p ) . _ _ l e n 
 
                   i f   h   t h e n 
 
                       - -   c a l l   t h e   h a n d l e r   w i t h   t h e   o p e r a n d 
 
                       r e t u r n   ( h ( o p ) ) 
 
                   e l s e     - -   n o   h a n d l e r   a v a i l a b l e :   d e f a u l t   b e h a v i o r 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 S e e    2 . 5 . 5   f o r   a   d e s c r i p t i o n   o f   t h e   l e n g t h   o f   a   t a b l e .   
 
 
 
 " e q " :   t h e   = =   o p e r a t i o n .   T h e   f u n c t i o n   g e t c o m p h a n d l e r   d e f i n e s   h o w   L u a   c h o o s e s   a   m e t a m e t h o d   f o r   c o m p a r i s o n   o p e r a t o r s .   A   
 
 m e t a m e t h o d   o n l y   i s   s e l e c t e d   w h e n   b o t h   o b j e c t s   b e i n g   c o m p a r e d   h a v e   t h e   s a m e   t y p e   a n d   t h e   s a m e   m e t a m e t h o d   f o r   t h e   s e l e c t e d   
 
 o p e r a t i o n .   
 
           f u n c t i o n   g e t c o m p h a n d l e r   ( o p 1 ,   o p 2 ,   e v e n t ) 
 
               i f   t y p e ( o p 1 )   ~ =   t y p e ( o p 2 )   t h e n   r e t u r n   n i l   e n d 
 
               l o c a l   m m 1   =   m e t a t a b l e ( o p 1 ) [ e v e n t ] 
 
               l o c a l   m m 2   =   m e t a t a b l e ( o p 2 ) [ e v e n t ] 
 
               i f   m m 1   = =   m m 2   t h e n   r e t u r n   m m 1   e l s e   r e t u r n   n i l   e n d 
 
           e n d 
 
 
 
 T h e   " e q "   e v e n t   i s   d e f i n e d   a s   f o l l o w s :   
 
 
 
           f u n c t i o n   e q _ e v e n t   ( o p 1 ,   o p 2 ) 
 
               i f   t y p e ( o p 1 )   ~ =   t y p e ( o p 2 )   t h e n     - -   d i f f e r e n t   t y p e s ? 
 
                   r e t u r n   f a l s e       - -   d i f f e r e n t   o b j e c t s 
 
               e n d 
 
               i f   o p 1   = =   o p 2   t h e n       - -   p r i m i t i v e   e q u a l ? 
 
                   r e t u r n   t r u e       - -   o b j e c t s   a r e   e q u a l 
 
               e n d 
 
               - -   t r y   m e t a m e t h o d 
 
               l o c a l   h   =   g e t c o m p h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ e q " ) 
 
               i f   h   t h e n 
 
                   r e t u r n   ( h ( o p 1 ,   o p 2 ) ) 
 
               e l s e 
 
                   r e t u r n   f a l s e 
 
               e n d 
 
           e n d 
 
 
 
 a   ~ =   b   i s   e q u i v a l e n t   t o   n o t   ( a   = =   b ) .   
 
 
 
 " l t " :   t h e   <   o p e r a t i o n .   
 
           f u n c t i o n   l t _ e v e n t   ( o p 1 ,   o p 2 ) 
 
               i f   t y p e ( o p 1 )   = =   " n u m b e r "   a n d   t y p e ( o p 2 )   = =   " n u m b e r "   t h e n 
 
                   r e t u r n   o p 1   <   o p 2       - -   n u m e r i c   c o m p a r i s o n 
 
               e l s e i f   t y p e ( o p 1 )   = =   " s t r i n g "   a n d   t y p e ( o p 2 )   = =   " s t r i n g "   t h e n 
 
                   r e t u r n   o p 1   <   o p 2       - -   l e x i c o g r a p h i c   c o m p a r i s o n 
 
               e l s e 
 
                   l o c a l   h   =   g e t c o m p h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ l t " ) 
 
                   i f   h   t h e n 
 
                       r e t u r n   ( h ( o p 1 ,   o p 2 ) ) 
 
                   e l s e 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 a   >   b   i s   e q u i v a l e n t   t o   b   <   a .   
 
 
 
 " l e " :   t h e   < =   o p e r a t i o n .   
 
           f u n c t i o n   l e _ e v e n t   ( o p 1 ,   o p 2 ) 
 
               i f   t y p e ( o p 1 )   = =   " n u m b e r "   a n d   t y p e ( o p 2 )   = =   " n u m b e r "   t h e n 
 
                   r e t u r n   o p 1   < =   o p 2       - -   n u m e r i c   c o m p a r i s o n 
 
               e l s e i f   t y p e ( o p 1 )   = =   " s t r i n g "   a n d   t y p e ( o p 2 )   = =   " s t r i n g "   t h e n 
 
                   r e t u r n   o p 1   < =   o p 2       - -   l e x i c o g r a p h i c   c o m p a r i s o n 
 
               e l s e 
 
                   l o c a l   h   =   g e t c o m p h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ l e " ) 
 
                   i f   h   t h e n 
 
                       r e t u r n   ( h ( o p 1 ,   o p 2 ) ) 
 
                   e l s e 
 
                       h   =   g e t c o m p h a n d l e r ( o p 1 ,   o p 2 ,   " _ _ l t " ) 
 
                       i f   h   t h e n 
 
                           r e t u r n   n o t   h ( o p 2 ,   o p 1 ) 
 
                       e l s e 
 
                           e r r o r (    ) 
 
                       e n d 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 a   > =   b   i s   e q u i v a l e n t   t o   b   < =   a .   N o t e   t h a t ,   i n   t h e   a b s e n c e   o f   a   " l e "   m e t a m e t h o d ,   L u a   t r i e s   t h e   " l t " ,   a s s u m i n g   t h a t   a   < =   b   i s   
 
 e q u i v a l e n t   t o   n o t   ( b   <   a ) .   
 
 
 
 " i n d e x " :   T h e   i n d e x i n g   a c c e s s   t a b l e [ k e y ] .   
 
           f u n c t i o n   g e t t a b l e _ e v e n t   ( t a b l e ,   k e y ) 
 
               l o c a l   h 
 
               i f   t y p e ( t a b l e )   = =   " t a b l e "   t h e n 
 
                   l o c a l   v   =   r a w g e t ( t a b l e ,   k e y ) 
 
                   i f   v   ~ =   n i l   t h e n   r e t u r n   v   e n d 
 
                   h   =   m e t a t a b l e ( t a b l e ) . _ _ i n d e x 
 
                   i f   h   = =   n i l   t h e n   r e t u r n   n i l   e n d 
 
               e l s e 
 
                   h   =   m e t a t a b l e ( t a b l e ) . _ _ i n d e x 
 
                   i f   h   = =   n i l   t h e n 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
               i f   t y p e ( h )   = =   " f u n c t i o n "   t h e n 
 
                   r e t u r n   ( h ( t a b l e ,   k e y ) )           - -   c a l l   t h e   h a n d l e r 
 
               e l s e   r e t u r n   h [ k e y ]                       - -   o r   r e p e a t   o p e r a t i o n   o n   i t 
 
               e n d 
 
           e n d 
 
 
 
 
 
 " n e w i n d e x " :   T h e   i n d e x i n g   a s s i g n m e n t   t a b l e [ k e y ]   =   v a l u e .   
 
           f u n c t i o n   s e t t a b l e _ e v e n t   ( t a b l e ,   k e y ,   v a l u e ) 
 
               l o c a l   h 
 
               i f   t y p e ( t a b l e )   = =   " t a b l e "   t h e n 
 
                   l o c a l   v   =   r a w g e t ( t a b l e ,   k e y ) 
 
                   i f   v   ~ =   n i l   t h e n   r a w s e t ( t a b l e ,   k e y ,   v a l u e ) ;   r e t u r n   e n d 
 
                   h   =   m e t a t a b l e ( t a b l e ) . _ _ n e w i n d e x 
 
                   i f   h   = =   n i l   t h e n   r a w s e t ( t a b l e ,   k e y ,   v a l u e ) ;   r e t u r n   e n d 
 
               e l s e 
 
                   h   =   m e t a t a b l e ( t a b l e ) . _ _ n e w i n d e x 
 
                   i f   h   = =   n i l   t h e n 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
               i f   t y p e ( h )   = =   " f u n c t i o n "   t h e n 
 
                   h ( t a b l e ,   k e y , v a l u e )                       - -   c a l l   t h e   h a n d l e r 
 
               e l s e   h [ k e y ]   =   v a l u e                           - -   o r   r e p e a t   o p e r a t i o n   o n   i t 
 
               e n d 
 
           e n d 
 
 
 
 
 
 " c a l l " :   c a l l e d   w h e n   L u a   c a l l s   a   v a l u e .   
 
           f u n c t i o n   f u n c t i o n _ e v e n t   ( f u n c ,   . . . ) 
 
               i f   t y p e ( f u n c )   = =   " f u n c t i o n "   t h e n 
 
                   r e t u r n   f u n c ( . . . )       - -   p r i m i t i v e   c a l l 
 
               e l s e 
 
                   l o c a l   h   =   m e t a t a b l e ( f u n c ) . _ _ c a l l 
 
                   i f   h   t h e n 
 
                       r e t u r n   h ( f u n c ,   . . . ) 
 
                   e l s e 
 
                       e r r o r (    ) 
 
                   e n d 
 
               e n d 
 
           e n d 
 
 
 
 
 
 2 . 9   -   E n v i r o n m e n t s 
 
 B e s i d e s   m e t a t a b l e s ,   o b j e c t s   o f   t y p e s   t h r e a d ,   f u n c t i o n ,   a n d   u s e r d a t a   h a v e   a n o t h e r   t a b l e   a s s o c i a t e d   w i t h   t h e m ,   c a l l e d   t h e i r   
 
 e n v i r o n m e n t .   L i k e   m e t a t a b l e s ,   e n v i r o n m e n t s   a r e   r e g u l a r   t a b l e s   a n d   m u l t i p l e   o b j e c t s   c a n   s h a r e   t h e   s a m e   e n v i r o n m e n t .   
 
 
 
 T h r e a d s   a r e   c r e a t e d   s h a r i n g   t h e   e n v i r o n m e n t   o f   t h e   c r e a t i n g   t h r e a d .   U s e r d a t a   a n d   C   f u n c t i o n s   a r e   c r e a t e d   s h a r i n g   t h e   
 
 e n v i r o n m e n t   o f   t h e   c r e a t i n g   C   f u n c t i o n .   N o n - n e s t e d   L u a   f u n c t i o n s   ( c r e a t e d   b y   l o a d f i l e ,   l o a d s t r i n g   o r   l o a d )   a r e   c r e a t e d   
 
 s h a r i n g   t h e   e n v i r o n m e n t   o f   t h e   c r e a t i n g   t h r e a d .   N e s t e d   L u a   f u n c t i o n s   a r e   c r e a t e d   s h a r i n g   t h e   e n v i r o n m e n t   o f   t h e   c r e a t i n g   
 
 L u a   f u n c t i o n .   
 
 
 
 E n v i r o n m e n t s   a s s o c i a t e d   w i t h   u s e r d a t a   h a v e   n o   m e a n i n g   f o r   L u a .   I t   i s   o n l y   a   c o n v e n i e n c e   f e a t u r e   f o r   p r o g r a m m e r s   t o   
 
 a s s o c i a t e   a   t a b l e   t o   a   u s e r d a t a .   
 
 
 
 E n v i r o n m e n t s   a s s o c i a t e d   w i t h   t h r e a d s   a r e   c a l l e d   g l o b a l   e n v i r o n m e n t s .   T h e y   a r e   u s e d   a s   t h e   d e f a u l t   e n v i r o n m e n t   f o r   t h r e a d s   
 
 a n d   n o n - n e s t e d   L u a   f u n c t i o n s   c r e a t e d   b y   t h e   t h r e a d   a n d   c a n   b e   d i r e c t l y   a c c e s s e d   b y   C   c o d e   ( s e e    3 . 3 ) .   
 
 
 
 T h e   e n v i r o n m e n t   a s s o c i a t e d   w i t h   a   C   f u n c t i o n   c a n   b e   d i r e c t l y   a c c e s s e d   b y   C   c o d e   ( s e e    3 . 3 ) .   I t   i s   u s e d   a s   t h e   d e f a u l t   
 
 e n v i r o n m e n t   f o r   o t h e r   C   f u n c t i o n s   a n d   u s e r d a t a   c r e a t e d   b y   t h e   f u n c t i o n .   
 
 
 
 E n v i r o n m e n t s   a s s o c i a t e d   w i t h   L u a   f u n c t i o n s   a r e   u s e d   t o   r e s o l v e   a l l   a c c e s s e s   t o   g l o b a l   v a r i a b l e s   w i t h i n   t h e   f u n c t i o n   
 
 ( s e e    2 . 3 ) .   T h e y   a r e   u s e d   a s   t h e   d e f a u l t   e n v i r o n m e n t   f o r   n e s t e d   L u a   f u n c t i o n s   c r e a t e d   b y   t h e   f u n c t i o n .   
 
 
 
 Y o u   c a n   c h a n g e   t h e   e n v i r o n m e n t   o f   a   L u a   f u n c t i o n   o r   t h e   r u n n i n g   t h r e a d   b y   c a l l i n g   s e t f e n v .   Y o u   c a n   g e t   t h e   e n v i r o n m e n t   o f   a   
 
 L u a   f u n c t i o n   o r   t h e   r u n n i n g   t h r e a d   b y   c a l l i n g   g e t f e n v .   T o   m a n i p u l a t e   t h e   e n v i r o n m e n t   o f   o t h e r   o b j e c t s   ( u s e r d a t a ,   
 
 C   f u n c t i o n s ,   o t h e r   t h r e a d s )   y o u   m u s t   u s e   t h e   C   A P I .   
 
 
 
 2 . 1 0   -   G a r b a g e   C o l l e c t i o n 
 
 L u a   p e r f o r m s   a u t o m a t i c   m e m o r y   m a n a g e m e n t .   T h i s   m e a n s   t h a t   y o u   h a v e   t o   w o r r y   n e i t h e r   a b o u t   a l l o c a t i n g   m e m o r y   f o r   n e w   o b j e c t s   
 
 n o r   a b o u t   f r e e i n g   i t   w h e n   t h e   o b j e c t s   a r e   n o   l o n g e r   n e e d e d .   L u a   m a n a g e s   m e m o r y   a u t o m a t i c a l l y   b y   r u n n i n g   a   g a r b a g e   c o l l e c t o r   
 
 f r o m   t i m e   t o   t i m e   t o   c o l l e c t   a l l   d e a d   o b j e c t s   ( t h a t   i s ,   o b j e c t s   t h a t   a r e   n o   l o n g e r   a c c e s s i b l e   f r o m   L u a ) .   A l l   m e m o r y   u s e d   b y   
 
 L u a   i s   s u b j e c t   t o   a u t o m a t i c   m a n a g e m e n t :   t a b l e s ,   u s e r d a t a ,   f u n c t i o n s ,   t h r e a d s ,   s t r i n g s ,   e t c .   
 
 
 
 L u a   i m p l e m e n t s   a n   i n c r e m e n t a l   m a r k - a n d - s w e e p   c o l l e c t o r .   I t   u s e s   t w o   n u m b e r s   t o   c o n t r o l   i t s   g a r b a g e - c o l l e c t i o n   c y c l e s :   t h e   
 
 g a r b a g e - c o l l e c t o r   p a u s e   a n d   t h e   g a r b a g e - c o l l e c t o r   s t e p   m u l t i p l i e r .   B o t h   u s e   p e r c e n t a g e   p o i n t s   a s   u n i t s   ( s o   t h a t   a   v a l u e   o f   
 
 1 0 0   m e a n s   a n   i n t e r n a l   v a l u e   o f   1 ) .   
 
 
 
 T h e   g a r b a g e - c o l l e c t o r   p a u s e   c o n t r o l s   h o w   l o n g   t h e   c o l l e c t o r   w a i t s   b e f o r e   s t a r t i n g   a   n e w   c y c l e .   L a r g e r   v a l u e s   m a k e   t h e   
 
 c o l l e c t o r   l e s s   a g g r e s s i v e .   V a l u e s   s m a l l e r   t h a n   1 0 0   m e a n   t h e   c o l l e c t o r   w i l l   n o t   w a i t   t o   s t a r t   a   n e w   c y c l e .   A   v a l u e   o f   2 0 0   
 
 m e a n s   t h a t   t h e   c o l l e c t o r   w a i t s   f o r   t h e   t o t a l   m e m o r y   i n   u s e   t o   d o u b l e   b e f o r e   s t a r t i n g   a   n e w   c y c l e .   
 
 
 
 T h e   s t e p   m u l t i p l i e r   c o n t r o l s   t h e   r e l a t i v e   s p e e d   o f   t h e   c o l l e c t o r   r e l a t i v e   t o   m e m o r y   a l l o c a t i o n .   L a r g e r   v a l u e s   m a k e   t h e   
 
 c o l l e c t o r   m o r e   a g g r e s s i v e   b u t   a l s o   i n c r e a s e   t h e   s i z e   o f   e a c h   i n c r e m e n t a l   s t e p .   V a l u e s   s m a l l e r   t h a n   1 0 0   m a k e   t h e   c o l l e c t o r   
 
 t o o   s l o w   a n d   c a n   r e s u l t   i n   t h e   c o l l e c t o r   n e v e r   f i n i s h i n g   a   c y c l e .   T h e   d e f a u l t ,   2 0 0 ,   m e a n s   t h a t   t h e   c o l l e c t o r   r u n s   a t   
 
 " t w i c e "   t h e   s p e e d   o f   m e m o r y   a l l o c a t i o n .   
 
 
 
 Y o u   c a n   c h a n g e   t h e s e   n u m b e r s   b y   c a l l i n g   l u a _ g c   i n   C   o r   c o l l e c t g a r b a g e   i n   L u a .   W i t h   t h e s e   f u n c t i o n s   y o u   c a n   a l s o   c o n t r o l   t h e   
 
 c o l l e c t o r   d i r e c t l y   ( e . g . ,   s t o p   a n d   r e s t a r t   i t ) .   
 
 
 
 2 . 1 0 . 1   -   G a r b a g e - C o l l e c t i o n   M e t a m e t h o d s 
 
 U s i n g   t h e   C   A P I ,   y o u   c a n   s e t   g a r b a g e - c o l l e c t o r   m e t a m e t h o d s   f o r   u s e r d a t a   ( s e e    2 . 8 ) .   T h e s e   m e t a m e t h o d s   a r e   a l s o   c a l l e d   
 
 f i n a l i z e r s .   F i n a l i z e r s   a l l o w   y o u   t o   c o o r d i n a t e   L u a ' s   g a r b a g e   c o l l e c t i o n   w i t h   e x t e r n a l   r e s o u r c e   m a n a g e m e n t   ( s u c h   a s   c l o s i n g   
 
 f i l e s ,   n e t w o r k   o r   d a t a b a s e   c o n n e c t i o n s ,   o r   f r e e i n g   y o u r   o w n   m e m o r y ) .   
 
 
 
 G a r b a g e   u s e r d a t a   w i t h   a   f i e l d   _ _ g c   i n   t h e i r   m e t a t a b l e s   a r e   n o t   c o l l e c t e d   i m m e d i a t e l y   b y   t h e   g a r b a g e   c o l l e c t o r .   I n s t e a d ,   L u a   
 
 p u t s   t h e m   i n   a   l i s t .   A f t e r   t h e   c o l l e c t i o n ,   L u a   d o e s   t h e   e q u i v a l e n t   o f   t h e   f o l l o w i n g   f u n c t i o n   f o r   e a c h   u s e r d a t a   i n   t h a t   
 
 l i s t :   
 
 
 
           f u n c t i o n   g c _ e v e n t   ( u d a t a ) 
 
               l o c a l   h   =   m e t a t a b l e ( u d a t a ) . _ _ g c 
 
               i f   h   t h e n 
 
                   h ( u d a t a ) 
 
               e n d 
 
           e n d 
 
 
 
 A t   t h e   e n d   o f   e a c h   g a r b a g e - c o l l e c t i o n   c y c l e ,   t h e   f i n a l i z e r s   f o r   u s e r d a t a   a r e   c a l l e d   i n   r e v e r s e   o r d e r   o f   t h e i r   c r e a t i o n ,   
 
 a m o n g   t h o s e   c o l l e c t e d   i n   t h a t   c y c l e .   T h a t   i s ,   t h e   f i r s t   f i n a l i z e r   t o   b e   c a l l e d   i s   t h e   o n e   a s s o c i a t e d   w i t h   t h e   u s e r d a t a   
 
 c r e a t e d   l a s t   i n   t h e   p r o g r a m .   T h e   u s e r d a t a   i t s e l f   i s   f r e e d   o n l y   i n   t h e   n e x t   g a r b a g e - c o l l e c t i o n   c y c l e .   
 
 
 
 2 . 1 0 . 2   -   W e a k   T a b l e s 
 
 A   w e a k   t a b l e   i s   a   t a b l e   w h o s e   e l e m e n t s   a r e   w e a k   r e f e r e n c e s .   A   w e a k   r e f e r e n c e   i s   i g n o r e d   b y   t h e   g a r b a g e   c o l l e c t o r .   I n   o t h e r   
 
 w o r d s ,   i f   t h e   o n l y   r e f e r e n c e s   t o   a n   o b j e c t   a r e   w e a k   r e f e r e n c e s ,   t h e n   t h e   g a r b a g e   c o l l e c t o r   w i l l   c o l l e c t   t h i s   o b j e c t .   
 
 
 
 A   w e a k   t a b l e   c a n   h a v e   w e a k   k e y s ,   w e a k   v a l u e s ,   o r   b o t h .   A   t a b l e   w i t h   w e a k   k e y s   a l l o w s   t h e   c o l l e c t i o n   o f   i t s   k e y s ,   b u t   
 
 p r e v e n t s   t h e   c o l l e c t i o n   o f   i t s   v a l u e s .   A   t a b l e   w i t h   b o t h   w e a k   k e y s   a n d   w e a k   v a l u e s   a l l o w s   t h e   c o l l e c t i o n   o f   b o t h   k e y s   a n d   
 
 v a l u e s .   I n   a n y   c a s e ,   i f   e i t h e r   t h e   k e y   o r   t h e   v a l u e   i s   c o l l e c t e d ,   t h e   w h o l e   p a i r   i s   r e m o v e d   f r o m   t h e   t a b l e .   T h e   w e a k n e s s   o f   
 
 a   t a b l e   i s   c o n t r o l l e d   b y   t h e   _ _ m o d e   f i e l d   o f   i t s   m e t a t a b l e .   I f   t h e   _ _ m o d e   f i e l d   i s   a   s t r i n g   c o n t a i n i n g   t h e   c h a r a c t e r   ' k ' ,   
 
 t h e   k e y s   i n   t h e   t a b l e   a r e   w e a k .   I f   _ _ m o d e   c o n t a i n s   ' v ' ,   t h e   v a l u e s   i n   t h e   t a b l e   a r e   w e a k .   
 
 
 
 A f t e r   y o u   u s e   a   t a b l e   a s   a   m e t a t a b l e ,   y o u   s h o u l d   n o t   c h a n g e   t h e   v a l u e   o f   i t s   _ _ m o d e   f i e l d .   O t h e r w i s e ,   t h e   w e a k   b e h a v i o r   o f   
 
 t h e   t a b l e s   c o n t r o l l e d   b y   t h i s   m e t a t a b l e   i s   u n d e f i n e d .   
 
 
 
 2 . 1 1   -   C o r o u t i n e s 
 
 L u a   s u p p o r t s   c o r o u t i n e s ,   a l s o   c a l l e d   c o l l a b o r a t i v e   m u l t i t h r e a d i n g .   A   c o r o u t i n e   i n   L u a   r e p r e s e n t s   a n   i n d e p e n d e n t   t h r e a d   o f   
 
 e x e c u t i o n .   U n l i k e   t h r e a d s   i n   m u l t i t h r e a d   s y s t e m s ,   h o w e v e r ,   a   c o r o u t i n e   o n l y   s u s p e n d s   i t s   e x e c u t i o n   b y   e x p l i c i t l y   c a l l i n g   a   
 
 y i e l d   f u n c t i o n .   
 
 
 
 Y o u   c r e a t e   a   c o r o u t i n e   w i t h   a   c a l l   t o   c o r o u t i n e . c r e a t e .   I t s   s o l e   a r g u m e n t   i s   a   f u n c t i o n   t h a t   i s   t h e   m a i n   f u n c t i o n   o f   t h e   
 
 c o r o u t i n e .   T h e   c r e a t e   f u n c t i o n   o n l y   c r e a t e s   a   n e w   c o r o u t i n e   a n d   r e t u r n s   a   h a n d l e   t o   i t   ( a n   o b j e c t   o f   t y p e   t h r e a d ) ;   i t   d o e s   
 
 n o t   s t a r t   t h e   c o r o u t i n e   e x e c u t i o n .   
 
 
 
 W h e n   y o u   f i r s t   c a l l   c o r o u t i n e . r e s u m e ,   p a s s i n g   a s   i t s   f i r s t   a r g u m e n t   a   t h r e a d   r e t u r n e d   b y   c o r o u t i n e . c r e a t e ,   t h e   c o r o u t i n e   
 
 s t a r t s   i t s   e x e c u t i o n ,   a t   t h e   f i r s t   l i n e   o f   i t s   m a i n   f u n c t i o n .   E x t r a   a r g u m e n t s   p a s s e d   t o   c o r o u t i n e . r e s u m e   a r e   p a s s e d   o n   t o   
 
 t h e   c o r o u t i n e   m a i n   f u n c t i o n .   A f t e r   t h e   c o r o u t i n e   s t a r t s   r u n n i n g ,   i t   r u n s   u n t i l   i t   t e r m i n a t e s   o r   y i e l d s .   
 
 
 
 A   c o r o u t i n e   c a n   t e r m i n a t e   i t s   e x e c u t i o n   i n   t w o   w a y s :   n o r m a l l y ,   w h e n   i t s   m a i n   f u n c t i o n   r e t u r n s   ( e x p l i c i t l y   o r   i m p l i c i t l y ,   
 
 a f t e r   t h e   l a s t   i n s t r u c t i o n ) ;   a n d   a b n o r m a l l y ,   i f   t h e r e   i s   a n   u n p r o t e c t e d   e r r o r .   I n   t h e   f i r s t   c a s e ,   c o r o u t i n e . r e s u m e   r e t u r n s   
 
 t r u e ,   p l u s   a n y   v a l u e s   r e t u r n e d   b y   t h e   c o r o u t i n e   m a i n   f u n c t i o n .   I n   c a s e   o f   e r r o r s ,   c o r o u t i n e . r e s u m e   r e t u r n s   f a l s e   p l u s   a n   
 
 e r r o r   m e s s a g e .   
 
 
 
 A   c o r o u t i n e   y i e l d s   b y   c a l l i n g   c o r o u t i n e . y i e l d .   W h e n   a   c o r o u t i n e   y i e l d s ,   t h e   c o r r e s p o n d i n g   c o r o u t i n e . r e s u m e   r e t u r n s   
 
 i m m e d i a t e l y ,   e v e n   i f   t h e   y i e l d   h a p p e n s   i n s i d e   n e s t e d   f u n c t i o n   c a l l s   ( t h a t   i s ,   n o t   i n   t h e   m a i n   f u n c t i o n ,   b u t   i n   a   f u n c t i o n   
 
 d i r e c t l y   o r   i n d i r e c t l y   c a l l e d   b y   t h e   m a i n   f u n c t i o n ) .   I n   t h e   c a s e   o f   a   y i e l d ,   c o r o u t i n e . r e s u m e   a l s o   r e t u r n s   t r u e ,   p l u s   a n y   
 
 v a l u e s   p a s s e d   t o   c o r o u t i n e . y i e l d .   T h e   n e x t   t i m e   y o u   r e s u m e   t h e   s a m e   c o r o u t i n e ,   i t   c o n t i n u e s   i t s   e x e c u t i o n   f r o m   t h e   p o i n t   
 
 w h e r e   i t   y i e l d e d ,   w i t h   t h e   c a l l   t o   c o r o u t i n e . y i e l d   r e t u r n i n g   a n y   e x t r a   a r g u m e n t s   p a s s e d   t o   c o r o u t i n e . r e s u m e .   
 
 
 
 L i k e   c o r o u t i n e . c r e a t e ,   t h e   c o r o u t i n e . w r a p   f u n c t i o n   a l s o   c r e a t e s   a   c o r o u t i n e ,   b u t   i n s t e a d   o f   r e t u r n i n g   t h e   c o r o u t i n e   i t s e l f ,   
 
 i t   r e t u r n s   a   f u n c t i o n   t h a t ,   w h e n   c a l l e d ,   r e s u m e s   t h e   c o r o u t i n e .   A n y   a r g u m e n t s   p a s s e d   t o   t h i s   f u n c t i o n   g o   a s   e x t r a   a r g u m e n t s   
 
 t o   c o r o u t i n e . r e s u m e .   c o r o u t i n e . w r a p   r e t u r n s   a l l   t h e   v a l u e s   r e t u r n e d   b y   c o r o u t i n e . r e s u m e ,   e x c e p t   t h e   f i r s t   o n e   ( t h e   b o o l e a n   
 
 e r r o r   c o d e ) .   U n l i k e   c o r o u t i n e . r e s u m e ,   c o r o u t i n e . w r a p   d o e s   n o t   c a t c h   e r r o r s ;   a n y   e r r o r   i s   p r o p a g a t e d   t o   t h e   c a l l e r .   
 
 
 
 A s   a n   e x a m p l e ,   c o n s i d e r   t h e   f o l l o w i n g   c o d e :   
 
 
 
           f u n c t i o n   f o o   ( a ) 
 
               p r i n t ( " f o o " ,   a ) 
 
               r e t u r n   c o r o u t i n e . y i e l d ( 2 * a ) 
 
           e n d 
 
           
 
           c o   =   c o r o u t i n e . c r e a t e ( f u n c t i o n   ( a , b ) 
 
                       p r i n t ( " c o - b o d y " ,   a ,   b ) 
 
                       l o c a l   r   =   f o o ( a + 1 ) 
 
                       p r i n t ( " c o - b o d y " ,   r ) 
 
                       l o c a l   r ,   s   =   c o r o u t i n e . y i e l d ( a + b ,   a - b ) 
 
                       p r i n t ( " c o - b o d y " ,   r ,   s ) 
 
                       r e t u r n   b ,   " e n d " 
 
           e n d ) 
 
                         
 
           p r i n t ( " m a i n " ,   c o r o u t i n e . r e s u m e ( c o ,   1 ,   1 0 ) ) 
 
           p r i n t ( " m a i n " ,   c o r o u t i n e . r e s u m e ( c o ,   " r " ) ) 
 
           p r i n t ( " m a i n " ,   c o r o u t i n e . r e s u m e ( c o ,   " x " ,   " y " ) ) 
 
           p r i n t ( " m a i n " ,   c o r o u t i n e . r e s u m e ( c o ,   " x " ,   " y " ) ) 
 
 
 
 W h e n   y o u   r u n   i t ,   i t   p r o d u c e s   t h e   f o l l o w i n g   o u t p u t :   
 
 
 
           c o - b o d y   1               1 0 
 
           f o o           2 
 
           
 
           m a i n         t r u e         4 
 
           c o - b o d y   r 
 
           m a i n         t r u e         1 1             - 9 
 
           c o - b o d y   x               y 
 
           m a i n         t r u e         1 0             e n d 
 
           m a i n         f a l s e       c a n n o t   r e s u m e   d e a d   c o r o u t i n e 
 
 
 
 3   -   T h e   A p p l i c a t i o n   P r o g r a m   I n t e r f a c e 
 
 T h i s   s e c t i o n   d e s c r i b e s   t h e   C   A P I   f o r   L u a ,   t h a t   i s ,   t h e   s e t   o f   C   f u n c t i o n s   a v a i l a b l e   t o   t h e   h o s t   p r o g r a m   t o   c o m m u n i c a t e   w i t h   
 
 L u a .   A l l   A P I   f u n c t i o n s   a n d   r e l a t e d   t y p e s   a n d   c o n s t a n t s   a r e   d e c l a r e d   i n   t h e   h e a d e r   f i l e   l u a . h .   
 
 
 
 E v e n   w h e n   w e   u s e   t h e   t e r m   " f u n c t i o n " ,   a n y   f a c i l i t y   i n   t h e   A P I   m a y   b e   p r o v i d e d   a s   a   m a c r o   i n s t e a d .   A l l   s u c h   m a c r o s   u s e   e a c h   
 
 o f   t h e i r   a r g u m e n t s   e x a c t l y   o n c e   ( e x c e p t   f o r   t h e   f i r s t   a r g u m e n t ,   w h i c h   i s   a l w a y s   a   L u a   s t a t e ) ,   a n d   s o   d o   n o t   g e n e r a t e   a n y   
 
 h i d d e n   s i d e - e f f e c t s .   
 
 
 
 A s   i n   m o s t   C   l i b r a r i e s ,   t h e   L u a   A P I   f u n c t i o n s   d o   n o t   c h e c k   t h e i r   a r g u m e n t s   f o r   v a l i d i t y   o r   c o n s i s t e n c y .   H o w e v e r ,   y o u   c a n   
 
 c h a n g e   t h i s   b e h a v i o r   b y   c o m p i l i n g   L u a   w i t h   a   p r o p e r   d e f i n i t i o n   f o r   t h e   m a c r o   l u a i _ a p i c h e c k ,   i n   f i l e   l u a c o n f . h .   
 
 
 
 3 . 1   -   T h e   S t a c k 
 
 L u a   u s e s   a   v i r t u a l   s t a c k   t o   p a s s   v a l u e s   t o   a n d   f r o m   C .   E a c h   e l e m e n t   i n   t h i s   s t a c k   r e p r e s e n t s   a   L u a   v a l u e   ( n i l ,   n u m b e r ,   
 
 s t r i n g ,   e t c . ) .   
 
 
 
 W h e n e v e r   L u a   c a l l s   C ,   t h e   c a l l e d   f u n c t i o n   g e t s   a   n e w   s t a c k ,   w h i c h   i s   i n d e p e n d e n t   o f   p r e v i o u s   s t a c k s   a n d   o f   s t a c k s   o f   C   
 
 f u n c t i o n s   t h a t   a r e   s t i l l   a c t i v e .   T h i s   s t a c k   i n i t i a l l y   c o n t a i n s   a n y   a r g u m e n t s   t o   t h e   C   f u n c t i o n   a n d   i t   i s   w h e r e   t h e   C   
 
 f u n c t i o n   p u s h e s   i t s   r e s u l t s   t o   b e   r e t u r n e d   t o   t h e   c a l l e r   ( s e e   l u a _ C F u n c t i o n ) .   
 
 
 
 F o r   c o n v e n i e n c e ,   m o s t   q u e r y   o p e r a t i o n s   i n   t h e   A P I   d o   n o t   f o l l o w   a   s t r i c t   s t a c k   d i s c i p l i n e .   I n s t e a d ,   t h e y   c a n   r e f e r   t o   a n y   
 
 e l e m e n t   i n   t h e   s t a c k   b y   u s i n g   a n   i n d e x :   A   p o s i t i v e   i n d e x   r e p r e s e n t s   a n   a b s o l u t e   s t a c k   p o s i t i o n   ( s t a r t i n g   a t   1 ) ;   a   n e g a t i v e   
 
 i n d e x   r e p r e s e n t s   a n   o f f s e t   r e l a t i v e   t o   t h e   t o p   o f   t h e   s t a c k .   M o r e   s p e c i f i c a l l y ,   i f   t h e   s t a c k   h a s   n   e l e m e n t s ,   t h e n   i n d e x   1   
 
 r e p r e s e n t s   t h e   f i r s t   e l e m e n t   ( t h a t   i s ,   t h e   e l e m e n t   t h a t   w a s   p u s h e d   o n t o   t h e   s t a c k   f i r s t )   a n d   i n d e x   n   r e p r e s e n t s   t h e   l a s t   
 
 e l e m e n t ;   i n d e x   - 1   a l s o   r e p r e s e n t s   t h e   l a s t   e l e m e n t   ( t h a t   i s ,   t h e   e l e m e n t   a t   t h e   t o p )   a n d   i n d e x   - n   r e p r e s e n t s   t h e   f i r s t   
 
 e l e m e n t .   W e   s a y   t h a t   a n   i n d e x   i s   v a l i d   i f   i t   l i e s   b e t w e e n   1   a n d   t h e   s t a c k   t o p   ( t h a t   i s ,   i f   1   d"  a b s ( i n d e x )   d"  t o p ) .   
 
 
 
 3 . 2   -   S t a c k   S i z e 
 
 W h e n   y o u   i n t e r a c t   w i t h   L u a   A P I ,   y o u   a r e   r e s p o n s i b l e   f o r   e n s u r i n g   c o n s i s t e n c y .   I n   p a r t i c u l a r ,   y o u   a r e   r e s p o n s i b l e   f o r   
 
 c o n t r o l l i n g   s t a c k   o v e r f l o w .   Y o u   c a n   u s e   t h e   f u n c t i o n   l u a _ c h e c k s t a c k   t o   g r o w   t h e   s t a c k   s i z e .   
 
 
 
 W h e n e v e r   L u a   c a l l s   C ,   i t   e n s u r e s   t h a t   a t   l e a s t   L U A _ M I N S T A C K   s t a c k   p o s i t i o n s   a r e   a v a i l a b l e .   L U A _ M I N S T A C K   i s   d e f i n e d   a s   2 0 ,   
 
 s o   t h a t   u s u a l l y   y o u   d o   n o t   h a v e   t o   w o r r y   a b o u t   s t a c k   s p a c e   u n l e s s   y o u r   c o d e   h a s   l o o p s   p u s h i n g   e l e m e n t s   o n t o   t h e   s t a c k .   
 
 
 
 M o s t   q u e r y   f u n c t i o n s   a c c e p t   a s   i n d i c e s   a n y   v a l u e   i n s i d e   t h e   a v a i l a b l e   s t a c k   s p a c e ,   t h a t   i s ,   i n d i c e s   u p   t o   t h e   m a x i m u m   s t a c k   
 
 s i z e   y o u   h a v e   s e t   t h r o u g h   l u a _ c h e c k s t a c k .   S u c h   i n d i c e s   a r e   c a l l e d   a c c e p t a b l e   i n d i c e s .   M o r e   f o r m a l l y ,   w e   d e f i n e   a n   
 
 a c c e p t a b l e   i n d e x   a s   f o l l o w s :   
 
 
 
           ( i n d e x   <   0   & &   a b s ( i n d e x )   < =   t o p )   | | 
 
           ( i n d e x   >   0   & &   i n d e x   < =   s t a c k s p a c e ) 
 
 
 
 N o t e   t h a t   0   i s   n e v e r   a n   a c c e p t a b l e   i n d e x .   
 
 
 
 3 . 3   -   P s e u d o - I n d i c e s 
 
 U n l e s s   o t h e r w i s e   n o t e d ,   a n y   f u n c t i o n   t h a t   a c c e p t s   v a l i d   i n d i c e s   c a n   a l s o   b e   c a l l e d   w i t h   p s e u d o - i n d i c e s ,   w h i c h   r e p r e s e n t   
 
 s o m e   L u a   v a l u e s   t h a t   a r e   a c c e s s i b l e   t o   C   c o d e   b u t   w h i c h   a r e   n o t   i n   t h e   s t a c k .   P s e u d o - i n d i c e s   a r e   u s e d   t o   a c c e s s   t h e   t h r e a d   
 
 e n v i r o n m e n t ,   t h e   f u n c t i o n   e n v i r o n m e n t ,   t h e   r e g i s t r y ,   a n d   t h e   u p v a l u e s   o f   a   C   f u n c t i o n   ( s e e    3 . 4 ) .   
 
 
 
 T h e   t h r e a d   e n v i r o n m e n t   ( w h e r e   g l o b a l   v a r i a b l e s   l i v e )   i s   a l w a y s   a t   p s e u d o - i n d e x   L U A _ G L O B A L S I N D E X .   T h e   e n v i r o n m e n t   o f   t h e   
 
 r u n n i n g   C   f u n c t i o n   i s   a l w a y s   a t   p s e u d o - i n d e x   L U A _ E N V I R O N I N D E X .   
 
 
 
 T o   a c c e s s   a n d   c h a n g e   t h e   v a l u e   o f   g l o b a l   v a r i a b l e s ,   y o u   c a n   u s e   r e g u l a r   t a b l e   o p e r a t i o n s   o v e r   a n   e n v i r o n m e n t   t a b l e .   F o r   
 
 i n s t a n c e ,   t o   a c c e s s   t h e   v a l u e   o f   a   g l o b a l   v a r i a b l e ,   d o   
 
 
 
           l u a _ g e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   v a r n a m e ) ; 
 
 
 
 3 . 4   -   C   C l o s u r e s 
 
 W h e n   a   C   f u n c t i o n   i s   c r e a t e d ,   i t   i s   p o s s i b l e   t o   a s s o c i a t e   s o m e   v a l u e s   w i t h   i t ,   t h u s   c r e a t i n g   a   C   c l o s u r e ;   t h e s e   v a l u e s   a r e   
 
 c a l l e d   u p v a l u e s   a n d   a r e   a c c e s s i b l e   t o   t h e   f u n c t i o n   w h e n e v e r   i t   i s   c a l l e d   ( s e e   l u a _ p u s h c c l o s u r e ) .   
 
 
 
 W h e n e v e r   a   C   f u n c t i o n   i s   c a l l e d ,   i t s   u p v a l u e s   a r e   l o c a t e d   a t   s p e c i f i c   p s e u d o - i n d i c e s .   T h e s e   p s e u d o - i n d i c e s   a r e   p r o d u c e d   b y   
 
 t h e   m a c r o   l u a _ u p v a l u e i n d e x .   T h e   f i r s t   v a l u e   a s s o c i a t e d   w i t h   a   f u n c t i o n   i s   a t   p o s i t i o n   l u a _ u p v a l u e i n d e x ( 1 ) ,   a n d   s o   o n .   A n y   
 
 a c c e s s   t o   l u a _ u p v a l u e i n d e x ( n ) ,   w h e r e   n   i s   g r e a t e r   t h a n   t h e   n u m b e r   o f   u p v a l u e s   o f   t h e   c u r r e n t   f u n c t i o n   ( b u t   n o t   g r e a t e r   t h a n   
 
 2 5 6 ) ,   p r o d u c e s   a n   a c c e p t a b l e   ( b u t   i n v a l i d )   i n d e x .   
 
 
 
 3 . 5   -   R e g i s t r y 
 
 L u a   p r o v i d e s   a   r e g i s t r y ,   a   p r e - d e f i n e d   t a b l e   t h a t   c a n   b e   u s e d   b y   a n y   C   c o d e   t o   s t o r e   w h a t e v e r   L u a   v a l u e   i t   n e e d s   t o   s t o r e .   
 
 T h i s   t a b l e   i s   a l w a y s   l o c a t e d   a t   p s e u d o - i n d e x   L U A _ R E G I S T R Y I N D E X .   A n y   C   l i b r a r y   c a n   s t o r e   d a t a   i n t o   t h i s   t a b l e ,   b u t   i t   s h o u l d   
 
 t a k e   c a r e   t o   c h o o s e   k e y s   d i f f e r e n t   f r o m   t h o s e   u s e d   b y   o t h e r   l i b r a r i e s ,   t o   a v o i d   c o l l i s i o n s .   T y p i c a l l y ,   y o u   s h o u l d   u s e   a s   
 
 k e y   a   s t r i n g   c o n t a i n i n g   y o u r   l i b r a r y   n a m e   o r   a   l i g h t   u s e r d a t a   w i t h   t h e   a d d r e s s   o f   a   C   o b j e c t   i n   y o u r   c o d e .   
 
 
 
 T h e   i n t e g e r   k e y s   i n   t h e   r e g i s t r y   a r e   u s e d   b y   t h e   r e f e r e n c e   m e c h a n i s m ,   i m p l e m e n t e d   b y   t h e   a u x i l i a r y   l i b r a r y ,   a n d   t h e r e f o r e   
 
 s h o u l d   n o t   b e   u s e d   f o r   o t h e r   p u r p o s e s .   
 
 
 
 3 . 6   -   E r r o r   H a n d l i n g   i n   C 
 
 I n t e r n a l l y ,   L u a   u s e s   t h e   C   l o n g j m p   f a c i l i t y   t o   h a n d l e   e r r o r s .   ( Y o u   c a n   a l s o   c h o o s e   t o   u s e   e x c e p t i o n s   i f   y o u   u s e   C + + ;   s e e   
 
 f i l e   l u a c o n f . h . )   W h e n   L u a   f a c e s   a n y   e r r o r   ( s u c h   a s   m e m o r y   a l l o c a t i o n   e r r o r s ,   t y p e   e r r o r s ,   s y n t a x   e r r o r s ,   a n d   r u n t i m e   e r r o r s )   
 
 i t   r a i s e s   a n   e r r o r ;   t h a t   i s ,   i t   d o e s   a   l o n g   j u m p .   A   p r o t e c t e d   e n v i r o n m e n t   u s e s   s e t j m p   t o   s e t   a   r e c o v e r   p o i n t ;   a n y   e r r o r   
 
 j u m p s   t o   t h e   m o s t   r e c e n t   a c t i v e   r e c o v e r   p o i n t .   
 
 
 
 M o s t   f u n c t i o n s   i n   t h e   A P I   c a n   t h r o w   a n   e r r o r ,   f o r   i n s t a n c e   d u e   t o   a   m e m o r y   a l l o c a t i o n   e r r o r .   T h e   d o c u m e n t a t i o n   f o r   e a c h   
 
 f u n c t i o n   i n d i c a t e s   w h e t h e r   i t   c a n   t h r o w   e r r o r s .   
 
 
 
 I n s i d e   a   C   f u n c t i o n   y o u   c a n   t h r o w   a n   e r r o r   b y   c a l l i n g   l u a _ e r r o r .   
 
 
 
 3 . 7   -   F u n c t i o n s   a n d   T y p e s 
 
 H e r e   w e   l i s t   a l l   f u n c t i o n s   a n d   t y p e s   f r o m   t h e   C   A P I   i n   a l p h a b e t i c a l   o r d e r .   E a c h   f u n c t i o n   h a s   a n   i n d i c a t o r   l i k e   t h i s :   [ - o ,   
 
 + p ,   x ]   
 
 
 
 T h e   f i r s t   f i e l d ,   o ,   i s   h o w   m a n y   e l e m e n t s   t h e   f u n c t i o n   p o p s   f r o m   t h e   s t a c k .   T h e   s e c o n d   f i e l d ,   p ,   i s   h o w   m a n y   e l e m e n t s   t h e   
 
 f u n c t i o n   p u s h e s   o n t o   t h e   s t a c k .   ( A n y   f u n c t i o n   a l w a y s   p u s h e s   i t s   r e s u l t s   a f t e r   p o p p i n g   i t s   a r g u m e n t s . )   A   f i e l d   i n   t h e   f o r m   
 
 x | y   m e a n s   t h e   f u n c t i o n   c a n   p u s h   ( o r   p o p )   x   o r   y   e l e m e n t s ,   d e p e n d i n g   o n   t h e   s i t u a t i o n ;   a n   i n t e r r o g a t i o n   m a r k   ' ? '   m e a n s   t h a t   
 
 w e   c a n n o t   k n o w   h o w   m a n y   e l e m e n t s   t h e   f u n c t i o n   p o p s / p u s h e s   b y   l o o k i n g   o n l y   a t   i t s   a r g u m e n t s   ( e . g . ,   t h e y   m a y   d e p e n d   o n   w h a t   
 
 i s   o n   t h e   s t a c k ) .   T h e   t h i r d   f i e l d ,   x ,   t e l l s   w h e t h e r   t h e   f u n c t i o n   m a y   t h r o w   e r r o r s :   ' - '   m e a n s   t h e   f u n c t i o n   n e v e r   t h r o w s   a n y   
 
 e r r o r ;   ' m '   m e a n s   t h e   f u n c t i o n   m a y   t h r o w   a n   e r r o r   o n l y   d u e   t o   n o t   e n o u g h   m e m o r y ;   ' e '   m e a n s   t h e   f u n c t i o n   m a y   t h r o w   o t h e r   
 
 k i n d s   o f   e r r o r s ;   ' v '   m e a n s   t h e   f u n c t i o n   m a y   t h r o w   a n   e r r o r   o n   p u r p o s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ A l l o c 
 
 t y p e d e f   v o i d   *   ( * l u a _ A l l o c )   ( v o i d   * u d , 
 
                                                           v o i d   * p t r , 
 
                                                           s i z e _ t   o s i z e , 
 
                                                           s i z e _ t   n s i z e ) ; 
 
 T h e   t y p e   o f   t h e   m e m o r y - a l l o c a t i o n   f u n c t i o n   u s e d   b y   L u a   s t a t e s .   T h e   a l l o c a t o r   f u n c t i o n   m u s t   p r o v i d e   a   f u n c t i o n a l i t y   s i m i l a r   
 
 t o   r e a l l o c ,   b u t   n o t   e x a c t l y   t h e   s a m e .   I t s   a r g u m e n t s   a r e   u d ,   a n   o p a q u e   p o i n t e r   p a s s e d   t o   l u a _ n e w s t a t e ;   p t r ,   a   p o i n t e r   t o   t h e   
 
 b l o c k   b e i n g   a l l o c a t e d / r e a l l o c a t e d / f r e e d ;   o s i z e ,   t h e   o r i g i n a l   s i z e   o f   t h e   b l o c k ;   n s i z e ,   t h e   n e w   s i z e   o f   t h e   b l o c k .   p t r   i s   
 
 N U L L   i f   a n d   o n l y   i f   o s i z e   i s   z e r o .   W h e n   n s i z e   i s   z e r o ,   t h e   a l l o c a t o r   m u s t   r e t u r n   N U L L ;   i f   o s i z e   i s   n o t   z e r o ,   i t   s h o u l d   f r e e   
 
 t h e   b l o c k   p o i n t e d   t o   b y   p t r .   W h e n   n s i z e   i s   n o t   z e r o ,   t h e   a l l o c a t o r   r e t u r n s   N U L L   i f   a n d   o n l y   i f   i t   c a n n o t   f i l l   t h e   r e q u e s t .   
 
 W h e n   n s i z e   i s   n o t   z e r o   a n d   o s i z e   i s   z e r o ,   t h e   a l l o c a t o r   s h o u l d   b e h a v e   l i k e   m a l l o c .   W h e n   n s i z e   a n d   o s i z e   a r e   n o t   z e r o ,   t h e   
 
 a l l o c a t o r   b e h a v e s   l i k e   r e a l l o c .   L u a   a s s u m e s   t h a t   t h e   a l l o c a t o r   n e v e r   f a i l s   w h e n   o s i z e   > =   n s i z e .   
 
 
 
 H e r e   i s   a   s i m p l e   i m p l e m e n t a t i o n   f o r   t h e   a l l o c a t o r   f u n c t i o n .   I t   i s   u s e d   i n   t h e   a u x i l i a r y   l i b r a r y   b y   l u a L _ n e w s t a t e .   
 
 
 
           s t a t i c   v o i d   * l _ a l l o c   ( v o i d   * u d ,   v o i d   * p t r ,   s i z e _ t   o s i z e , 
 
                                                                                                 s i z e _ t   n s i z e )   { 
 
               ( v o i d ) u d ;     ( v o i d ) o s i z e ;     / *   n o t   u s e d   * / 
 
               i f   ( n s i z e   = =   0 )   { 
 
                   f r e e ( p t r ) ; 
 
                   r e t u r n   N U L L ; 
 
               } 
 
               e l s e 
 
                   r e t u r n   r e a l l o c ( p t r ,   n s i z e ) ; 
 
           } 
 
 
 
 T h i s   c o d e   a s s u m e s   t h a t   f r e e ( N U L L )   h a s   n o   e f f e c t   a n d   t h a t   r e a l l o c ( N U L L ,   s i z e )   i s   e q u i v a l e n t   t o   m a l l o c ( s i z e ) .   A N S I   C   e n s u r e s   
 
 b o t h   b e h a v i o r s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ a t p a n i c 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ C F u n c t i o n   l u a _ a t p a n i c   ( l u a _ S t a t e   * L ,   l u a _ C F u n c t i o n   p a n i c f ) ; 
 
 S e t s   a   n e w   p a n i c   f u n c t i o n   a n d   r e t u r n s   t h e   o l d   o n e .   
 
 
 
 I f   a n   e r r o r   h a p p e n s   o u t s i d e   a n y   p r o t e c t e d   e n v i r o n m e n t ,   L u a   c a l l s   a   p a n i c   f u n c t i o n   a n d   t h e n   c a l l s   e x i t ( E X I T _ F A I L U R E ) ,   t h u s   
 
 e x i t i n g   t h e   h o s t   a p p l i c a t i o n .   Y o u r   p a n i c   f u n c t i o n   c a n   a v o i d   t h i s   e x i t   b y   n e v e r   r e t u r n i n g   ( e . g . ,   d o i n g   a   l o n g   j u m p ) .   
 
 
 
 T h e   p a n i c   f u n c t i o n   c a n   a c c e s s   t h e   e r r o r   m e s s a g e   a t   t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c a l l 
 
 [ - ( n a r g s   +   1 ) ,   + n r e s u l t s ,   e ]   
 
 
 
 v o i d   l u a _ c a l l   ( l u a _ S t a t e   * L ,   i n t   n a r g s ,   i n t   n r e s u l t s ) ; 
 
 C a l l s   a   f u n c t i o n .   
 
 
 
 T o   c a l l   a   f u n c t i o n   y o u   m u s t   u s e   t h e   f o l l o w i n g   p r o t o c o l :   f i r s t ,   t h e   f u n c t i o n   t o   b e   c a l l e d   i s   p u s h e d   o n t o   t h e   s t a c k ;   t h e n ,   
 
 t h e   a r g u m e n t s   t o   t h e   f u n c t i o n   a r e   p u s h e d   i n   d i r e c t   o r d e r ;   t h a t   i s ,   t h e   f i r s t   a r g u m e n t   i s   p u s h e d   f i r s t .   F i n a l l y   y o u   c a l l   
 
 l u a _ c a l l ;   n a r g s   i s   t h e   n u m b e r   o f   a r g u m e n t s   t h a t   y o u   p u s h e d   o n t o   t h e   s t a c k .   A l l   a r g u m e n t s   a n d   t h e   f u n c t i o n   v a l u e   a r e   p o p p e d   
 
 f r o m   t h e   s t a c k   w h e n   t h e   f u n c t i o n   i s   c a l l e d .   T h e   f u n c t i o n   r e s u l t s   a r e   p u s h e d   o n t o   t h e   s t a c k   w h e n   t h e   f u n c t i o n   r e t u r n s .   T h e   
 
 n u m b e r   o f   r e s u l t s   i s   a d j u s t e d   t o   n r e s u l t s ,   u n l e s s   n r e s u l t s   i s   L U A _ M U L T R E T .   I n   t h i s   c a s e ,   a l l   r e s u l t s   f r o m   t h e   f u n c t i o n   a r e   
 
 p u s h e d .   L u a   t a k e s   c a r e   t h a t   t h e   r e t u r n e d   v a l u e s   f i t   i n t o   t h e   s t a c k   s p a c e .   T h e   f u n c t i o n   r e s u l t s   a r e   p u s h e d   o n t o   t h e   s t a c k   i n   
 
 d i r e c t   o r d e r   ( t h e   f i r s t   r e s u l t   i s   p u s h e d   f i r s t ) ,   s o   t h a t   a f t e r   t h e   c a l l   t h e   l a s t   r e s u l t   i s   o n   t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 A n y   e r r o r   i n s i d e   t h e   c a l l e d   f u n c t i o n   i s   p r o p a g a t e d   u p w a r d s   ( w i t h   a   l o n g j m p ) .   
 
 
 
 T h e   f o l l o w i n g   e x a m p l e   s h o w s   h o w   t h e   h o s t   p r o g r a m   c a n   d o   t h e   e q u i v a l e n t   t o   t h i s   L u a   c o d e :   
 
 
 
           a   =   f ( " h o w " ,   t . x ,   1 4 ) 
 
 
 
 H e r e   i t   i s   i n   C :   
 
 
 
           l u a _ g e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   " f " ) ;   / *   f u n c t i o n   t o   b e   c a l l e d   * / 
 
           l u a _ p u s h s t r i n g ( L ,   " h o w " ) ;                                                 / *   1 s t   a r g u m e n t   * / 
 
           l u a _ g e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   " t " ) ;       / *   t a b l e   t o   b e   i n d e x e d   * / 
 
           l u a _ g e t f i e l d ( L ,   - 1 ,   " x " ) ;                 / *   p u s h   r e s u l t   o f   t . x   ( 2 n d   a r g )   * / 
 
           l u a _ r e m o v e ( L ,   - 2 ) ;                                     / *   r e m o v e   ' t '   f r o m   t h e   s t a c k   * / 
 
           l u a _ p u s h i n t e g e r ( L ,   1 4 ) ;                                                     / *   3 r d   a r g u m e n t   * / 
 
           l u a _ c a l l ( L ,   3 ,   1 ) ;           / *   c a l l   ' f '   w i t h   3   a r g u m e n t s   a n d   1   r e s u l t   * / 
 
           l u a _ s e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   " a " ) ;                 / *   s e t   g l o b a l   ' a '   * / 
 
 
 
 N o t e   t h a t   t h e   c o d e   a b o v e   i s   " b a l a n c e d " :   a t   i t s   e n d ,   t h e   s t a c k   i s   b a c k   t o   i t s   o r i g i n a l   c o n f i g u r a t i o n .   T h i s   i s   c o n s i d e r e d   
 
 g o o d   p r o g r a m m i n g   p r a c t i c e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ C F u n c t i o n 
 
 t y p e d e f   i n t   ( * l u a _ C F u n c t i o n )   ( l u a _ S t a t e   * L ) ; 
 
 T y p e   f o r   C   f u n c t i o n s .   
 
 
 
 I n   o r d e r   t o   c o m m u n i c a t e   p r o p e r l y   w i t h   L u a ,   a   C   f u n c t i o n   m u s t   u s e   t h e   f o l l o w i n g   p r o t o c o l ,   w h i c h   d e f i n e s   t h e   w a y   p a r a m e t e r s   
 
 a n d   r e s u l t s   a r e   p a s s e d :   a   C   f u n c t i o n   r e c e i v e s   i t s   a r g u m e n t s   f r o m   L u a   i n   i t s   s t a c k   i n   d i r e c t   o r d e r   ( t h e   f i r s t   a r g u m e n t   i s   
 
 p u s h e d   f i r s t ) .   S o ,   w h e n   t h e   f u n c t i o n   s t a r t s ,   l u a _ g e t t o p ( L )   r e t u r n s   t h e   n u m b e r   o f   a r g u m e n t s   r e c e i v e d   b y   t h e   f u n c t i o n .   T h e   
 
 f i r s t   a r g u m e n t   ( i f   a n y )   i s   a t   i n d e x   1   a n d   i t s   l a s t   a r g u m e n t   i s   a t   i n d e x   l u a _ g e t t o p ( L ) .   T o   r e t u r n   v a l u e s   t o   L u a ,   a   C   f u n c t i o n   
 
 j u s t   p u s h e s   t h e m   o n t o   t h e   s t a c k ,   i n   d i r e c t   o r d e r   ( t h e   f i r s t   r e s u l t   i s   p u s h e d   f i r s t ) ,   a n d   r e t u r n s   t h e   n u m b e r   o f   r e s u l t s .   A n y   
 
 o t h e r   v a l u e   i n   t h e   s t a c k   b e l o w   t h e   r e s u l t s   w i l l   b e   p r o p e r l y   d i s c a r d e d   b y   L u a .   L i k e   a   L u a   f u n c t i o n ,   a   C   f u n c t i o n   c a l l e d   b y   
 
 L u a   c a n   a l s o   r e t u r n   m a n y   r e s u l t s .   
 
 
 
 A s   a n   e x a m p l e ,   t h e   f o l l o w i n g   f u n c t i o n   r e c e i v e s   a   v a r i a b l e   n u m b e r   o f   n u m e r i c a l   a r g u m e n t s   a n d   r e t u r n s   t h e i r   a v e r a g e   a n d   s u m :   
 
 
 
           s t a t i c   i n t   f o o   ( l u a _ S t a t e   * L )   { 
 
               i n t   n   =   l u a _ g e t t o p ( L ) ;         / *   n u m b e r   o f   a r g u m e n t s   * / 
 
               l u a _ N u m b e r   s u m   =   0 ; 
 
               i n t   i ; 
 
               f o r   ( i   =   1 ;   i   < =   n ;   i + + )   { 
 
                   i f   ( ! l u a _ i s n u m b e r ( L ,   i ) )   { 
 
                       l u a _ p u s h s t r i n g ( L ,   " i n c o r r e c t   a r g u m e n t " ) ; 
 
                       l u a _ e r r o r ( L ) ; 
 
                   } 
 
                   s u m   + =   l u a _ t o n u m b e r ( L ,   i ) ; 
 
               } 
 
               l u a _ p u s h n u m b e r ( L ,   s u m / n ) ;                 / *   f i r s t   r e s u l t   * / 
 
               l u a _ p u s h n u m b e r ( L ,   s u m ) ;                   / *   s e c o n d   r e s u l t   * / 
 
               r e t u r n   2 ;                                       / *   n u m b e r   o f   r e s u l t s   * / 
 
           } 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c h e c k s t a c k 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 i n t   l u a _ c h e c k s t a c k   ( l u a _ S t a t e   * L ,   i n t   e x t r a ) ; 
 
 E n s u r e s   t h a t   t h e r e   a r e   a t   l e a s t   e x t r a   f r e e   s t a c k   s l o t s   i n   t h e   s t a c k .   I t   r e t u r n s   f a l s e   i f   i t   c a n n o t   g r o w   t h e   s t a c k   t o   t h a t   
 
 s i z e .   T h i s   f u n c t i o n   n e v e r   s h r i n k s   t h e   s t a c k ;   i f   t h e   s t a c k   i s   a l r e a d y   l a r g e r   t h a n   t h e   n e w   s i z e ,   i t   i s   l e f t   u n c h a n g e d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c l o s e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a _ c l o s e   ( l u a _ S t a t e   * L ) ; 
 
 D e s t r o y s   a l l   o b j e c t s   i n   t h e   g i v e n   L u a   s t a t e   ( c a l l i n g   t h e   c o r r e s p o n d i n g   g a r b a g e - c o l l e c t i o n   m e t a m e t h o d s ,   i f   a n y )   a n d   f r e e s   
 
 a l l   d y n a m i c   m e m o r y   u s e d   b y   t h i s   s t a t e .   O n   s e v e r a l   p l a t f o r m s ,   y o u   m a y   n o t   n e e d   t o   c a l l   t h i s   f u n c t i o n ,   b e c a u s e   a l l   r e s o u r c e s   
 
 a r e   n a t u r a l l y   r e l e a s e d   w h e n   t h e   h o s t   p r o g r a m   e n d s .   O n   t h e   o t h e r   h a n d ,   l o n g - r u n n i n g   p r o g r a m s ,   s u c h   a s   a   d a e m o n   o r   a   w e b   
 
 s e r v e r ,   m i g h t   n e e d   t o   r e l e a s e   s t a t e s   a s   s o o n   a s   t h e y   a r e   n o t   n e e d e d ,   t o   a v o i d   g r o w i n g   t o o   l a r g e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c o n c a t 
 
 [ - n ,   + 1 ,   e ]   
 
 
 
 v o i d   l u a _ c o n c a t   ( l u a _ S t a t e   * L ,   i n t   n ) ; 
 
 C o n c a t e n a t e s   t h e   n   v a l u e s   a t   t h e   t o p   o f   t h e   s t a c k ,   p o p s   t h e m ,   a n d   l e a v e s   t h e   r e s u l t   a t   t h e   t o p .   I f   n   i s   1 ,   t h e   r e s u l t   i s   
 
 t h e   s i n g l e   v a l u e   o n   t h e   s t a c k   ( t h a t   i s ,   t h e   f u n c t i o n   d o e s   n o t h i n g ) ;   i f   n   i s   0 ,   t h e   r e s u l t   i s   t h e   e m p t y   s t r i n g .   
 
 C o n c a t e n a t i o n   i s   p e r f o r m e d   f o l l o w i n g   t h e   u s u a l   s e m a n t i c s   o f   L u a   ( s e e    2 . 5 . 4 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c p c a l l 
 
 [ - 0 ,   + ( 0 | 1 ) ,   - ]   
 
 
 
 i n t   l u a _ c p c a l l   ( l u a _ S t a t e   * L ,   l u a _ C F u n c t i o n   f u n c ,   v o i d   * u d ) ; 
 
 C a l l s   t h e   C   f u n c t i o n   f u n c   i n   p r o t e c t e d   m o d e .   f u n c   s t a r t s   w i t h   o n l y   o n e   e l e m e n t   i n   i t s   s t a c k ,   a   l i g h t   u s e r d a t a   c o n t a i n i n g   
 
 u d .   I n   c a s e   o f   e r r o r s ,   l u a _ c p c a l l   r e t u r n s   t h e   s a m e   e r r o r   c o d e s   a s   l u a _ p c a l l ,   p l u s   t h e   e r r o r   o b j e c t   o n   t h e   t o p   o f   t h e   s t a c k ;   
 
 o t h e r w i s e ,   i t   r e t u r n s   z e r o ,   a n d   d o e s   n o t   c h a n g e   t h e   s t a c k .   A l l   v a l u e s   r e t u r n e d   b y   f u n c   a r e   d i s c a r d e d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ c r e a t e t a b l e 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ c r e a t e t a b l e   ( l u a _ S t a t e   * L ,   i n t   n a r r ,   i n t   n r e c ) ; 
 
 C r e a t e s   a   n e w   e m p t y   t a b l e   a n d   p u s h e s   i t   o n t o   t h e   s t a c k .   T h e   n e w   t a b l e   h a s   s p a c e   p r e - a l l o c a t e d   f o r   n a r r   a r r a y   e l e m e n t s   a n d   
 
 n r e c   n o n - a r r a y   e l e m e n t s .   T h i s   p r e - a l l o c a t i o n   i s   u s e f u l   w h e n   y o u   k n o w   e x a c t l y   h o w   m a n y   e l e m e n t s   t h e   t a b l e   w i l l   h a v e .   
 
 O t h e r w i s e   y o u   c a n   u s e   t h e   f u n c t i o n   l u a _ n e w t a b l e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ d u m p 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 i n t   l u a _ d u m p   ( l u a _ S t a t e   * L ,   l u a _ W r i t e r   w r i t e r ,   v o i d   * d a t a ) ; 
 
 D u m p s   a   f u n c t i o n   a s   a   b i n a r y   c h u n k .   R e c e i v e s   a   L u a   f u n c t i o n   o n   t h e   t o p   o f   t h e   s t a c k   a n d   p r o d u c e s   a   b i n a r y   c h u n k   t h a t ,   i f   
 
 l o a d e d   a g a i n ,   r e s u l t s   i n   a   f u n c t i o n   e q u i v a l e n t   t o   t h e   o n e   d u m p e d .   A s   i t   p r o d u c e s   p a r t s   o f   t h e   c h u n k ,   l u a _ d u m p   c a l l s   
 
 f u n c t i o n   w r i t e r   ( s e e   l u a _ W r i t e r )   w i t h   t h e   g i v e n   d a t a   t o   w r i t e   t h e m .   
 
 
 
 T h e   v a l u e   r e t u r n e d   i s   t h e   e r r o r   c o d e   r e t u r n e d   b y   t h e   l a s t   c a l l   t o   t h e   w r i t e r ;   0   m e a n s   n o   e r r o r s .   
 
 
 
 T h i s   f u n c t i o n   d o e s   n o t   p o p   t h e   L u a   f u n c t i o n   f r o m   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ e q u a l 
 
 [ - 0 ,   + 0 ,   e ]   
 
 
 
 i n t   l u a _ e q u a l   ( l u a _ S t a t e   * L ,   i n t   i n d e x 1 ,   i n t   i n d e x 2 ) ; 
 
 R e t u r n s   1   i f   t h e   t w o   v a l u e s   i n   a c c e p t a b l e   i n d i c e s   i n d e x 1   a n d   i n d e x 2   a r e   e q u a l ,   f o l l o w i n g   t h e   s e m a n t i c s   o f   t h e   L u a   = =   
 
 o p e r a t o r   ( t h a t   i s ,   m a y   c a l l   m e t a m e t h o d s ) .   O t h e r w i s e   r e t u r n s   0 .   A l s o   r e t u r n s   0   i f   a n y   o f   t h e   i n d i c e s   i s   n o n   v a l i d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ e r r o r 
 
 [ - 1 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a _ e r r o r   ( l u a _ S t a t e   * L ) ; 
 
 G e n e r a t e s   a   L u a   e r r o r .   T h e   e r r o r   m e s s a g e   ( w h i c h   c a n   a c t u a l l y   b e   a   L u a   v a l u e   o f   a n y   t y p e )   m u s t   b e   o n   t h e   s t a c k   t o p .   T h i s   
 
 f u n c t i o n   d o e s   a   l o n g   j u m p ,   a n d   t h e r e f o r e   n e v e r   r e t u r n s .   ( s e e   l u a L _ e r r o r ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g c 
 
 [ - 0 ,   + 0 ,   e ]   
 
 
 
 i n t   l u a _ g c   ( l u a _ S t a t e   * L ,   i n t   w h a t ,   i n t   d a t a ) ; 
 
 C o n t r o l s   t h e   g a r b a g e   c o l l e c t o r .   
 
 
 
 T h i s   f u n c t i o n   p e r f o r m s   s e v e r a l   t a s k s ,   a c c o r d i n g   t o   t h e   v a l u e   o f   t h e   p a r a m e t e r   w h a t :   
 
 
 
 L U A _ G C S T O P :   s t o p s   t h e   g a r b a g e   c o l l e c t o r .   
 
 L U A _ G C R E S T A R T :   r e s t a r t s   t h e   g a r b a g e   c o l l e c t o r .   
 
 L U A _ G C C O L L E C T :   p e r f o r m s   a   f u l l   g a r b a g e - c o l l e c t i o n   c y c l e .   
 
 L U A _ G C C O U N T :   r e t u r n s   t h e   c u r r e n t   a m o u n t   o f   m e m o r y   ( i n   K b y t e s )   i n   u s e   b y   L u a .   
 
 L U A _ G C C O U N T B :   r e t u r n s   t h e   r e m a i n d e r   o f   d i v i d i n g   t h e   c u r r e n t   a m o u n t   o f   b y t e s   o f   m e m o r y   i n   u s e   b y   L u a   b y   1 0 2 4 .   
 
 L U A _ G C S T E P :   p e r f o r m s   a n   i n c r e m e n t a l   s t e p   o f   g a r b a g e   c o l l e c t i o n .   T h e   s t e p   " s i z e "   i s   c o n t r o l l e d   b y   d a t a   ( l a r g e r   v a l u e s   m e a n   
 
 m o r e   s t e p s )   i n   a   n o n - s p e c i f i e d   w a y .   I f   y o u   w a n t   t o   c o n t r o l   t h e   s t e p   s i z e   y o u   m u s t   e x p e r i m e n t a l l y   t u n e   t h e   v a l u e   o f   d a t a .   T h e   
 
 f u n c t i o n   r e t u r n s   1   i f   t h e   s t e p   f i n i s h e d   a   g a r b a g e - c o l l e c t i o n   c y c l e .   
 
 L U A _ G C S E T P A U S E :   s e t s   d a t a   a s   t h e   n e w   v a l u e   f o r   t h e   p a u s e   o f   t h e   c o l l e c t o r   ( s e e    2 . 1 0 ) .   T h e   f u n c t i o n   r e t u r n s   t h e   p r e v i o u s   
 
 v a l u e   o f   t h e   p a u s e .   
 
 L U A _ G C S E T S T E P M U L :   s e t s   d a t a   a s   t h e   n e w   v a l u e   f o r   t h e   s t e p   m u l t i p l i e r   o f   t h e   c o l l e c t o r   ( s e e    2 . 1 0 ) .   T h e   f u n c t i o n   r e t u r n s   t h e   
 
 p r e v i o u s   v a l u e   o f   t h e   s t e p   m u l t i p l i e r .   
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t a l l o c f 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ A l l o c   l u a _ g e t a l l o c f   ( l u a _ S t a t e   * L ,   v o i d   * * u d ) ; 
 
 R e t u r n s   t h e   m e m o r y - a l l o c a t i o n   f u n c t i o n   o f   a   g i v e n   s t a t e .   I f   u d   i s   n o t   N U L L ,   L u a   s t o r e s   i n   * u d   t h e   o p a q u e   p o i n t e r   p a s s e d   t o   
 
 l u a _ n e w s t a t e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t f e n v 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ g e t f e n v   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   e n v i r o n m e n t   t a b l e   o f   t h e   v a l u e   a t   t h e   g i v e n   i n d e x .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t f i e l d 
 
 [ - 0 ,   + 1 ,   e ]   
 
 
 
 v o i d   l u a _ g e t f i e l d   ( l u a _ S t a t e   * L ,   i n t   i n d e x ,   c o n s t   c h a r   * k ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   v a l u e   t [ k ] ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x .   A s   i n   L u a ,   t h i s   f u n c t i o n   m a y   t r i g g e r   a   
 
 m e t a m e t h o d   f o r   t h e   " i n d e x "   e v e n t   ( s e e    2 . 8 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t g l o b a l 
 
 [ - 0 ,   + 1 ,   e ]   
 
 
 
 v o i d   l u a _ g e t g l o b a l   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * n a m e ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   v a l u e   o f   t h e   g l o b a l   n a m e .   I t   i s   d e f i n e d   a s   a   m a c r o :   
 
 
 
           # d e f i n e   l u a _ g e t g l o b a l ( L , s )     l u a _ g e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   s ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t m e t a t a b l e 
 
 [ - 0 ,   + ( 0 | 1 ) ,   - ]   
 
 
 
 i n t   l u a _ g e t m e t a t a b l e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   m e t a t a b l e   o f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x .   I f   t h e   i n d e x   i s   n o t   v a l i d ,   o r   i f   t h e   v a l u e   
 
 d o e s   n o t   h a v e   a   m e t a t a b l e ,   t h e   f u n c t i o n   r e t u r n s   0   a n d   p u s h e s   n o t h i n g   o n   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t t a b l e 
 
 [ - 1 ,   + 1 ,   e ]   
 
 
 
 v o i d   l u a _ g e t t a b l e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   v a l u e   t [ k ] ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x   a n d   k   i s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   
 
 s t a c k .   
 
 
 
 T h i s   f u n c t i o n   p o p s   t h e   k e y   f r o m   t h e   s t a c k   ( p u t t i n g   t h e   r e s u l t i n g   v a l u e   i n   i t s   p l a c e ) .   A s   i n   L u a ,   t h i s   f u n c t i o n   m a y   t r i g g e r   
 
 a   m e t a m e t h o d   f o r   t h e   " i n d e x "   e v e n t   ( s e e    2 . 8 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t t o p 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ g e t t o p   ( l u a _ S t a t e   * L ) ; 
 
 R e t u r n s   t h e   i n d e x   o f   t h e   t o p   e l e m e n t   i n   t h e   s t a c k .   B e c a u s e   i n d i c e s   s t a r t   a t   1 ,   t h i s   r e s u l t   i s   e q u a l   t o   t h e   n u m b e r   o f   
 
 e l e m e n t s   i n   t h e   s t a c k   ( a n d   s o   0   m e a n s   a n   e m p t y   s t a c k ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i n s e r t 
 
 [ - 1 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ i n s e r t   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 M o v e s   t h e   t o p   e l e m e n t   i n t o   t h e   g i v e n   v a l i d   i n d e x ,   s h i f t i n g   u p   t h e   e l e m e n t s   a b o v e   t h i s   i n d e x   t o   o p e n   s p a c e .   C a n n o t   b e   c a l l e d   
 
 w i t h   a   p s e u d o - i n d e x ,   b e c a u s e   a   p s e u d o - i n d e x   i s   n o t   a n   a c t u a l   s t a c k   p o s i t i o n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ I n t e g e r 
 
 t y p e d e f   p t r d i f f _ t   l u a _ I n t e g e r ; 
 
 T h e   t y p e   u s e d   b y   t h e   L u a   A P I   t o   r e p r e s e n t   i n t e g r a l   v a l u e s .   
 
 
 
 B y   d e f a u l t   i t   i s   a   p t r d i f f _ t ,   w h i c h   i s   u s u a l l y   t h e   l a r g e s t   s i g n e d   i n t e g r a l   t y p e   t h e   m a c h i n e   h a n d l e s   " c o m f o r t a b l y " .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s b o o l e a n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s b o o l e a n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   h a s   t y p e   b o o l e a n ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s c f u n c t i o n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s c f u n c t i o n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   C   f u n c t i o n ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s f u n c t i o n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s f u n c t i o n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   f u n c t i o n   ( e i t h e r   C   o r   L u a ) ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s l i g h t u s e r d a t a 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s l i g h t u s e r d a t a   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   l i g h t   u s e r d a t a ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s n i l 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s n i l   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   n i l ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s n o n e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s n o n e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   n o t   v a l i d   ( t h a t   i s ,   i t   r e f e r s   t o   a n   e l e m e n t   o u t s i d e   t h e   c u r r e n t   s t a c k ) ,   a n d   0   
 
 o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s n o n e o r n i l 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s n o n e o r n i l   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   n o t   v a l i d   ( t h a t   i s ,   i t   r e f e r s   t o   a n   e l e m e n t   o u t s i d e   t h e   c u r r e n t   s t a c k )   o r   i f   t h e   
 
 v a l u e   a t   t h i s   i n d e x   i s   n i l ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s n u m b e r 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s n u m b e r   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   n u m b e r   o r   a   s t r i n g   c o n v e r t i b l e   t o   a   n u m b e r ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s s t r i n g 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s s t r i n g   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   s t r i n g   o r   a   n u m b e r   ( w h i c h   i s   a l w a y s   c o n v e r t i b l e   t o   a   s t r i n g ) ,   a n d   
 
 0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s t a b l e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s t a b l e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   t a b l e ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s t h r e a d 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s t h r e a d   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   t h r e a d ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ i s u s e r d a t a 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ i s u s e r d a t a   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   u s e r d a t a   ( e i t h e r   f u l l   o r   l i g h t ) ,   a n d   0   o t h e r w i s e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ l e s s t h a n 
 
 [ - 0 ,   + 0 ,   e ]   
 
 
 
 i n t   l u a _ l e s s t h a n   ( l u a _ S t a t e   * L ,   i n t   i n d e x 1 ,   i n t   i n d e x 2 ) ; 
 
 R e t u r n s   1   i f   t h e   v a l u e   a t   a c c e p t a b l e   i n d e x   i n d e x 1   i s   s m a l l e r   t h a n   t h e   v a l u e   a t   a c c e p t a b l e   i n d e x   i n d e x 2 ,   f o l l o w i n g   t h e   
 
 s e m a n t i c s   o f   t h e   L u a   <   o p e r a t o r   ( t h a t   i s ,   m a y   c a l l   m e t a m e t h o d s ) .   O t h e r w i s e   r e t u r n s   0 .   A l s o   r e t u r n s   0   i f   a n y   o f   t h e   i n d i c e s   
 
 i s   n o n   v a l i d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ l o a d 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 i n t   l u a _ l o a d   ( l u a _ S t a t e   * L , 
 
                             l u a _ R e a d e r   r e a d e r , 
 
                             v o i d   * d a t a , 
 
                             c o n s t   c h a r   * c h u n k n a m e ) ; 
 
 L o a d s   a   L u a   c h u n k .   I f   t h e r e   a r e   n o   e r r o r s ,   l u a _ l o a d   p u s h e s   t h e   c o m p i l e d   c h u n k   a s   a   L u a   f u n c t i o n   o n   t o p   o f   t h e   s t a c k .   
 
 O t h e r w i s e ,   i t   p u s h e s   a n   e r r o r   m e s s a g e .   T h e   r e t u r n   v a l u e s   o f   l u a _ l o a d   a r e :   
 
 
 
 0 :   n o   e r r o r s ;   
 
 L U A _ E R R S Y N T A X :   s y n t a x   e r r o r   d u r i n g   p r e - c o m p i l a t i o n ;   
 
 L U A _ E R R M E M :   m e m o r y   a l l o c a t i o n   e r r o r .   
 
 T h i s   f u n c t i o n   o n l y   l o a d s   a   c h u n k ;   i t   d o e s   n o t   r u n   i t .   
 
 
 
 l u a _ l o a d   a u t o m a t i c a l l y   d e t e c t s   w h e t h e r   t h e   c h u n k   i s   t e x t   o r   b i n a r y ,   a n d   l o a d s   i t   a c c o r d i n g l y   ( s e e   p r o g r a m   l u a c ) .   
 
 
 
 T h e   l u a _ l o a d   f u n c t i o n   u s e s   a   u s e r - s u p p l i e d   r e a d e r   f u n c t i o n   t o   r e a d   t h e   c h u n k   ( s e e   l u a _ R e a d e r ) .   T h e   d a t a   a r g u m e n t   i s   a n   
 
 o p a q u e   v a l u e   p a s s e d   t o   t h e   r e a d e r   f u n c t i o n .   
 
 
 
 T h e   c h u n k n a m e   a r g u m e n t   g i v e s   a   n a m e   t o   t h e   c h u n k ,   w h i c h   i s   u s e d   f o r   e r r o r   m e s s a g e s   a n d   i n   d e b u g   i n f o r m a t i o n   ( s e e    3 . 8 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ n e w s t a t e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ S t a t e   * l u a _ n e w s t a t e   ( l u a _ A l l o c   f ,   v o i d   * u d ) ; 
 
 C r e a t e s   a   n e w ,   i n d e p e n d e n t   s t a t e .   R e t u r n s   N U L L   i f   c a n n o t   c r e a t e   t h e   s t a t e   ( d u e   t o   l a c k   o f   m e m o r y ) .   T h e   a r g u m e n t   f   i s   t h e   
 
 a l l o c a t o r   f u n c t i o n ;   L u a   d o e s   a l l   m e m o r y   a l l o c a t i o n   f o r   t h i s   s t a t e   t h r o u g h   t h i s   f u n c t i o n .   T h e   s e c o n d   a r g u m e n t ,   u d ,   i s   a n   
 
 o p a q u e   p o i n t e r   t h a t   L u a   s i m p l y   p a s s e s   t o   t h e   a l l o c a t o r   i n   e v e r y   c a l l .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ n e w t a b l e 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ n e w t a b l e   ( l u a _ S t a t e   * L ) ; 
 
 C r e a t e s   a   n e w   e m p t y   t a b l e   a n d   p u s h e s   i t   o n t o   t h e   s t a c k .   I t   i s   e q u i v a l e n t   t o   l u a _ c r e a t e t a b l e ( L ,   0 ,   0 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ n e w t h r e a d 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 l u a _ S t a t e   * l u a _ n e w t h r e a d   ( l u a _ S t a t e   * L ) ; 
 
 C r e a t e s   a   n e w   t h r e a d ,   p u s h e s   i t   o n   t h e   s t a c k ,   a n d   r e t u r n s   a   p o i n t e r   t o   a   l u a _ S t a t e   t h a t   r e p r e s e n t s   t h i s   n e w   t h r e a d .   T h e   n e w   
 
 s t a t e   r e t u r n e d   b y   t h i s   f u n c t i o n   s h a r e s   w i t h   t h e   o r i g i n a l   s t a t e   a l l   g l o b a l   o b j e c t s   ( s u c h   a s   t a b l e s ) ,   b u t   h a s   a n   i n d e p e n d e n t   
 
 e x e c u t i o n   s t a c k .   
 
 
 
 T h e r e   i s   n o   e x p l i c i t   f u n c t i o n   t o   c l o s e   o r   t o   d e s t r o y   a   t h r e a d .   T h r e a d s   a r e   s u b j e c t   t o   g a r b a g e   c o l l e c t i o n ,   l i k e   a n y   L u a   o b j e c t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ n e w u s e r d a t a 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   * l u a _ n e w u s e r d a t a   ( l u a _ S t a t e   * L ,   s i z e _ t   s i z e ) ; 
 
 T h i s   f u n c t i o n   a l l o c a t e s   a   n e w   b l o c k   o f   m e m o r y   w i t h   t h e   g i v e n   s i z e ,   p u s h e s   o n t o   t h e   s t a c k   a   n e w   f u l l   u s e r d a t a   w i t h   t h e   b l o c k   
 
 a d d r e s s ,   a n d   r e t u r n s   t h i s   a d d r e s s .   
 
 
 
 U s e r d a t a   r e p r e s e n t   C   v a l u e s   i n   L u a .   A   f u l l   u s e r d a t a   r e p r e s e n t s   a   b l o c k   o f   m e m o r y .   I t   i s   a n   o b j e c t   ( l i k e   a   t a b l e ) :   y o u   m u s t   
 
 c r e a t e   i t ,   i t   c a n   h a v e   i t s   o w n   m e t a t a b l e ,   a n d   y o u   c a n   d e t e c t   w h e n   i t   i s   b e i n g   c o l l e c t e d .   A   f u l l   u s e r d a t a   i s   o n l y   e q u a l   t o   
 
 i t s e l f   ( u n d e r   r a w   e q u a l i t y ) .   
 
 
 
 W h e n   L u a   c o l l e c t s   a   f u l l   u s e r d a t a   w i t h   a   g c   m e t a m e t h o d ,   L u a   c a l l s   t h e   m e t a m e t h o d   a n d   m a r k s   t h e   u s e r d a t a   a s   f i n a l i z e d .   W h e n   
 
 t h i s   u s e r d a t a   i s   c o l l e c t e d   a g a i n   t h e n   L u a   f r e e s   i t s   c o r r e s p o n d i n g   m e m o r y .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ n e x t 
 
 [ - 1 ,   + ( 2 | 0 ) ,   e ]   
 
 
 
 i n t   l u a _ n e x t   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P o p s   a   k e y   f r o m   t h e   s t a c k ,   a n d   p u s h e s   a   k e y - v a l u e   p a i r   f r o m   t h e   t a b l e   a t   t h e   g i v e n   i n d e x   ( t h e   " n e x t "   p a i r   a f t e r   t h e   g i v e n   
 
 k e y ) .   I f   t h e r e   a r e   n o   m o r e   e l e m e n t s   i n   t h e   t a b l e ,   t h e n   l u a _ n e x t   r e t u r n s   0   ( a n d   p u s h e s   n o t h i n g ) .   
 
 
 
 A   t y p i c a l   t r a v e r s a l   l o o k s   l i k e   t h i s :   
 
 
 
           / *   t a b l e   i s   i n   t h e   s t a c k   a t   i n d e x   ' t '   * / 
 
           l u a _ p u s h n i l ( L ) ;     / *   f i r s t   k e y   * / 
 
           w h i l e   ( l u a _ n e x t ( L ,   t )   ! =   0 )   { 
 
               / *   u s e s   ' k e y '   ( a t   i n d e x   - 2 )   a n d   ' v a l u e '   ( a t   i n d e x   - 1 )   * / 
 
               p r i n t f ( " % s   -   % s \ n " , 
 
                             l u a _ t y p e n a m e ( L ,   l u a _ t y p e ( L ,   - 2 ) ) , 
 
                             l u a _ t y p e n a m e ( L ,   l u a _ t y p e ( L ,   - 1 ) ) ) ; 
 
               / *   r e m o v e s   ' v a l u e ' ;   k e e p s   ' k e y '   f o r   n e x t   i t e r a t i o n   * / 
 
               l u a _ p o p ( L ,   1 ) ; 
 
           } 
 
 
 
 W h i l e   t r a v e r s i n g   a   t a b l e ,   d o   n o t   c a l l   l u a _ t o l s t r i n g   d i r e c t l y   o n   a   k e y ,   u n l e s s   y o u   k n o w   t h a t   t h e   k e y   i s   a c t u a l l y   a   s t r i n g .   
 
 R e c a l l   t h a t   l u a _ t o l s t r i n g   c h a n g e s   t h e   v a l u e   a t   t h e   g i v e n   i n d e x ;   t h i s   c o n f u s e s   t h e   n e x t   c a l l   t o   l u a _ n e x t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ N u m b e r 
 
 t y p e d e f   d o u b l e   l u a _ N u m b e r ; 
 
 T h e   t y p e   o f   n u m b e r s   i n   L u a .   B y   d e f a u l t ,   i t   i s   d o u b l e ,   b u t   t h a t   c a n   b e   c h a n g e d   i n   l u a c o n f . h .   
 
 
 
 T h r o u g h   t h e   c o n f i g u r a t i o n   f i l e   y o u   c a n   c h a n g e   L u a   t o   o p e r a t e   w i t h   a n o t h e r   t y p e   f o r   n u m b e r s   ( e . g . ,   f l o a t   o r   l o n g ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ o b j l e n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 s i z e _ t   l u a _ o b j l e n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   t h e   " l e n g t h "   o f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x :   f o r   s t r i n g s ,   t h i s   i s   t h e   s t r i n g   l e n g t h ;   f o r   t a b l e s ,   t h i s   
 
 i s   t h e   r e s u l t   o f   t h e   l e n g t h   o p e r a t o r   ( ' # ' ) ;   f o r   u s e r d a t a ,   t h i s   i s   t h e   s i z e   o f   t h e   b l o c k   o f   m e m o r y   a l l o c a t e d   f o r   t h e   
 
 u s e r d a t a ;   f o r   o t h e r   v a l u e s ,   i t   i s   0 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p c a l l 
 
 [ - ( n a r g s   +   1 ) ,   + ( n r e s u l t s | 1 ) ,   - ]   
 
 
 
 i n t   l u a _ p c a l l   ( l u a _ S t a t e   * L ,   i n t   n a r g s ,   i n t   n r e s u l t s ,   i n t   e r r f u n c ) ; 
 
 C a l l s   a   f u n c t i o n   i n   p r o t e c t e d   m o d e .   
 
 
 
 B o t h   n a r g s   a n d   n r e s u l t s   h a v e   t h e   s a m e   m e a n i n g   a s   i n   l u a _ c a l l .   I f   t h e r e   a r e   n o   e r r o r s   d u r i n g   t h e   c a l l ,   l u a _ p c a l l   b e h a v e s   
 
 e x a c t l y   l i k e   l u a _ c a l l .   H o w e v e r ,   i f   t h e r e   i s   a n y   e r r o r ,   l u a _ p c a l l   c a t c h e s   i t ,   p u s h e s   a   s i n g l e   v a l u e   o n   t h e   s t a c k   ( t h e   e r r o r   
 
 m e s s a g e ) ,   a n d   r e t u r n s   a n   e r r o r   c o d e .   L i k e   l u a _ c a l l ,   l u a _ p c a l l   a l w a y s   r e m o v e s   t h e   f u n c t i o n   a n d   i t s   a r g u m e n t s   f r o m   t h e   s t a c k .   
 
 
 
 I f   e r r f u n c   i s   0 ,   t h e n   t h e   e r r o r   m e s s a g e   r e t u r n e d   o n   t h e   s t a c k   i s   e x a c t l y   t h e   o r i g i n a l   e r r o r   m e s s a g e .   O t h e r w i s e ,   e r r f u n c   i s   
 
 t h e   s t a c k   i n d e x   o f   a n   e r r o r   h a n d l e r   f u n c t i o n .   ( I n   t h e   c u r r e n t   i m p l e m e n t a t i o n ,   t h i s   i n d e x   c a n n o t   b e   a   p s e u d o - i n d e x . )   I n   c a s e   
 
 o f   r u n t i m e   e r r o r s ,   t h i s   f u n c t i o n   w i l l   b e   c a l l e d   w i t h   t h e   e r r o r   m e s s a g e   a n d   i t s   r e t u r n   v a l u e   w i l l   b e   t h e   m e s s a g e   r e t u r n e d   o n   
 
 t h e   s t a c k   b y   l u a _ p c a l l .   
 
 
 
 T y p i c a l l y ,   t h e   e r r o r   h a n d l e r   f u n c t i o n   i s   u s e d   t o   a d d   m o r e   d e b u g   i n f o r m a t i o n   t o   t h e   e r r o r   m e s s a g e ,   s u c h   a s   a   s t a c k   t r a c e b a c k .   
 
 S u c h   i n f o r m a t i o n   c a n n o t   b e   g a t h e r e d   a f t e r   t h e   r e t u r n   o f   l u a _ p c a l l ,   s i n c e   b y   t h e n   t h e   s t a c k   h a s   u n w o u n d .   
 
 
 
 T h e   l u a _ p c a l l   f u n c t i o n   r e t u r n s   0   i n   c a s e   o f   s u c c e s s   o r   o n e   o f   t h e   f o l l o w i n g   e r r o r   c o d e s   ( d e f i n e d   i n   l u a . h ) :   
 
 
 
 L U A _ E R R R U N :   a   r u n t i m e   e r r o r .   
 
 L U A _ E R R M E M :   m e m o r y   a l l o c a t i o n   e r r o r .   F o r   s u c h   e r r o r s ,   L u a   d o e s   n o t   c a l l   t h e   e r r o r   h a n d l e r   f u n c t i o n .   
 
 L U A _ E R R E R R :   e r r o r   w h i l e   r u n n i n g   t h e   e r r o r   h a n d l e r   f u n c t i o n .   
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p o p 
 
 [ - n ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a _ p o p   ( l u a _ S t a t e   * L ,   i n t   n ) ; 
 
 P o p s   n   e l e m e n t s   f r o m   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h b o o l e a n 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h b o o l e a n   ( l u a _ S t a t e   * L ,   i n t   b ) ; 
 
 P u s h e s   a   b o o l e a n   v a l u e   w i t h   v a l u e   b   o n t o   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h c c l o s u r e 
 
 [ - n ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ p u s h c c l o s u r e   ( l u a _ S t a t e   * L ,   l u a _ C F u n c t i o n   f n ,   i n t   n ) ; 
 
 P u s h e s   a   n e w   C   c l o s u r e   o n t o   t h e   s t a c k .   
 
 
 
 W h e n   a   C   f u n c t i o n   i s   c r e a t e d ,   i t   i s   p o s s i b l e   t o   a s s o c i a t e   s o m e   v a l u e s   w i t h   i t ,   t h u s   c r e a t i n g   a   C   c l o s u r e   ( s e e    3 . 4 ) ;   t h e s e   
 
 v a l u e s   a r e   t h e n   a c c e s s i b l e   t o   t h e   f u n c t i o n   w h e n e v e r   i t   i s   c a l l e d .   T o   a s s o c i a t e   v a l u e s   w i t h   a   C   f u n c t i o n ,   f i r s t   t h e s e   v a l u e s   
 
 s h o u l d   b e   p u s h e d   o n t o   t h e   s t a c k   ( w h e n   t h e r e   a r e   m u l t i p l e   v a l u e s ,   t h e   f i r s t   v a l u e   i s   p u s h e d   f i r s t ) .   T h e n   l u a _ p u s h c c l o s u r e   i s   
 
 c a l l e d   t o   c r e a t e   a n d   p u s h   t h e   C   f u n c t i o n   o n t o   t h e   s t a c k ,   w i t h   t h e   a r g u m e n t   n   t e l l i n g   h o w   m a n y   v a l u e s   s h o u l d   b e   a s s o c i a t e d   
 
 w i t h   t h e   f u n c t i o n .   l u a _ p u s h c c l o s u r e   a l s o   p o p s   t h e s e   v a l u e s   f r o m   t h e   s t a c k .   
 
 
 
 T h e   m a x i m u m   v a l u e   f o r   n   i s   2 5 5 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h c f u n c t i o n 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ p u s h c f u n c t i o n   ( l u a _ S t a t e   * L ,   l u a _ C F u n c t i o n   f ) ; 
 
 P u s h e s   a   C   f u n c t i o n   o n t o   t h e   s t a c k .   T h i s   f u n c t i o n   r e c e i v e s   a   p o i n t e r   t o   a   C   f u n c t i o n   a n d   p u s h e s   o n t o   t h e   s t a c k   a   L u a   v a l u e   
 
 o f   t y p e   f u n c t i o n   t h a t ,   w h e n   c a l l e d ,   i n v o k e s   t h e   c o r r e s p o n d i n g   C   f u n c t i o n .   
 
 
 
 A n y   f u n c t i o n   t o   b e   r e g i s t e r e d   i n   L u a   m u s t   f o l l o w   t h e   c o r r e c t   p r o t o c o l   t o   r e c e i v e   i t s   p a r a m e t e r s   a n d   r e t u r n   i t s   r e s u l t s   ( s e e   
 
 l u a _ C F u n c t i o n ) .   
 
 
 
 l u a _ p u s h c f u n c t i o n   i s   d e f i n e d   a s   a   m a c r o :   
 
 
 
           # d e f i n e   l u a _ p u s h c f u n c t i o n ( L , f )     l u a _ p u s h c c l o s u r e ( L , f , 0 ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h f s t r i n g 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 c o n s t   c h a r   * l u a _ p u s h f s t r i n g   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * f m t ,   . . . ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   a   f o r m a t t e d   s t r i n g   a n d   r e t u r n s   a   p o i n t e r   t o   t h i s   s t r i n g .   I t   i s   s i m i l a r   t o   t h e   C   f u n c t i o n   s p r i n t f ,   b u t   
 
 h a s   s o m e   i m p o r t a n t   d i f f e r e n c e s :   
 
 
 
 Y o u   d o   n o t   h a v e   t o   a l l o c a t e   s p a c e   f o r   t h e   r e s u l t :   t h e   r e s u l t   i s   a   L u a   s t r i n g   a n d   L u a   t a k e s   c a r e   o f   m e m o r y   a l l o c a t i o n   ( a n d   
 
 d e a l l o c a t i o n ,   t h r o u g h   g a r b a g e   c o l l e c t i o n ) .   
 
 T h e   c o n v e r s i o n   s p e c i f i e r s   a r e   q u i t e   r e s t r i c t e d .   T h e r e   a r e   n o   f l a g s ,   w i d t h s ,   o r   p r e c i s i o n s .   T h e   c o n v e r s i o n   s p e c i f i e r s   c a n   
 
 o n l y   b e   ' % % '   ( i n s e r t s   a   ' % '   i n   t h e   s t r i n g ) ,   ' % s '   ( i n s e r t s   a   z e r o - t e r m i n a t e d   s t r i n g ,   w i t h   n o   s i z e   r e s t r i c t i o n s ) ,   ' % f '   ( i n s e r t s   
 
 a   l u a _ N u m b e r ) ,   ' % p '   ( i n s e r t s   a   p o i n t e r   a s   a   h e x a d e c i m a l   n u m e r a l ) ,   ' % d '   ( i n s e r t s   a n   i n t ) ,   a n d   ' % c '   ( i n s e r t s   a n   i n t   a s   a   
 
 c h a r a c t e r ) .   
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h i n t e g e r 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h i n t e g e r   ( l u a _ S t a t e   * L ,   l u a _ I n t e g e r   n ) ; 
 
 P u s h e s   a   n u m b e r   w i t h   v a l u e   n   o n t o   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h l i g h t u s e r d a t a 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h l i g h t u s e r d a t a   ( l u a _ S t a t e   * L ,   v o i d   * p ) ; 
 
 P u s h e s   a   l i g h t   u s e r d a t a   o n t o   t h e   s t a c k .   
 
 
 
 U s e r d a t a   r e p r e s e n t   C   v a l u e s   i n   L u a .   A   l i g h t   u s e r d a t a   r e p r e s e n t s   a   p o i n t e r .   I t   i s   a   v a l u e   ( l i k e   a   n u m b e r ) :   y o u   d o   n o t   c r e a t e   
 
 i t ,   i t   h a s   n o   i n d i v i d u a l   m e t a t a b l e ,   a n d   i t   i s   n o t   c o l l e c t e d   ( a s   i t   w a s   n e v e r   c r e a t e d ) .   A   l i g h t   u s e r d a t a   i s   e q u a l   t o   " a n y "   
 
 l i g h t   u s e r d a t a   w i t h   t h e   s a m e   C   a d d r e s s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h l i t e r a l 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ p u s h l i t e r a l   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * s ) ; 
 
 T h i s   m a c r o   i s   e q u i v a l e n t   t o   l u a _ p u s h l s t r i n g ,   b u t   c a n   b e   u s e d   o n l y   w h e n   s   i s   a   l i t e r a l   s t r i n g .   I n   t h e s e   c a s e s ,   i t   
 
 a u t o m a t i c a l l y   p r o v i d e s   t h e   s t r i n g   l e n g t h .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h l s t r i n g 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ p u s h l s t r i n g   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * s ,   s i z e _ t   l e n ) ; 
 
 P u s h e s   t h e   s t r i n g   p o i n t e d   t o   b y   s   w i t h   s i z e   l e n   o n t o   t h e   s t a c k .   L u a   m a k e s   ( o r   r e u s e s )   a n   i n t e r n a l   c o p y   o f   t h e   g i v e n   s t r i n g ,   
 
 s o   t h e   m e m o r y   a t   s   c a n   b e   f r e e d   o r   r e u s e d   i m m e d i a t e l y   a f t e r   t h e   f u n c t i o n   r e t u r n s .   T h e   s t r i n g   c a n   c o n t a i n   e m b e d d e d   z e r o s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h n i l 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h n i l   ( l u a _ S t a t e   * L ) ; 
 
 P u s h e s   a   n i l   v a l u e   o n t o   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h n u m b e r 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h n u m b e r   ( l u a _ S t a t e   * L ,   l u a _ N u m b e r   n ) ; 
 
 P u s h e s   a   n u m b e r   w i t h   v a l u e   n   o n t o   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h s t r i n g 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a _ p u s h s t r i n g   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * s ) ; 
 
 P u s h e s   t h e   z e r o - t e r m i n a t e d   s t r i n g   p o i n t e d   t o   b y   s   o n t o   t h e   s t a c k .   L u a   m a k e s   ( o r   r e u s e s )   a n   i n t e r n a l   c o p y   o f   t h e   g i v e n   
 
 s t r i n g ,   s o   t h e   m e m o r y   a t   s   c a n   b e   f r e e d   o r   r e u s e d   i m m e d i a t e l y   a f t e r   t h e   f u n c t i o n   r e t u r n s .   T h e   s t r i n g   c a n n o t   c o n t a i n   
 
 e m b e d d e d   z e r o s ;   i t   i s   a s s u m e d   t o   e n d   a t   t h e   f i r s t   z e r o .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h t h r e a d 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 i n t   l u a _ p u s h t h r e a d   ( l u a _ S t a t e   * L ) ; 
 
 P u s h e s   t h e   t h r e a d   r e p r e s e n t e d   b y   L   o n t o   t h e   s t a c k .   R e t u r n s   1   i f   t h i s   t h r e a d   i s   t h e   m a i n   t h r e a d   o f   i t s   s t a t e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h v a l u e 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ p u s h v a l u e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P u s h e s   a   c o p y   o f   t h e   e l e m e n t   a t   t h e   g i v e n   v a l i d   i n d e x   o n t o   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ p u s h v f s t r i n g 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 c o n s t   c h a r   * l u a _ p u s h v f s t r i n g   ( l u a _ S t a t e   * L , 
 
                                                             c o n s t   c h a r   * f m t , 
 
                                                             v a _ l i s t   a r g p ) ; 
 
 E q u i v a l e n t   t o   l u a _ p u s h f s t r i n g ,   e x c e p t   t h a t   i t   r e c e i v e s   a   v a _ l i s t   i n s t e a d   o f   a   v a r i a b l e   n u m b e r   o f   a r g u m e n t s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r a w e q u a l 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ r a w e q u a l   ( l u a _ S t a t e   * L ,   i n t   i n d e x 1 ,   i n t   i n d e x 2 ) ; 
 
 R e t u r n s   1   i f   t h e   t w o   v a l u e s   i n   a c c e p t a b l e   i n d i c e s   i n d e x 1   a n d   i n d e x 2   a r e   p r i m i t i v e l y   e q u a l   ( t h a t   i s ,   w i t h o u t   c a l l i n g   
 
 m e t a m e t h o d s ) .   O t h e r w i s e   r e t u r n s   0 .   A l s o   r e t u r n s   0   i f   a n y   o f   t h e   i n d i c e s   a r e   n o n   v a l i d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r a w g e t 
 
 [ - 1 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ r a w g e t   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 S i m i l a r   t o   l u a _ g e t t a b l e ,   b u t   d o e s   a   r a w   a c c e s s   ( i . e . ,   w i t h o u t   m e t a m e t h o d s ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r a w g e t i 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a _ r a w g e t i   ( l u a _ S t a t e   * L ,   i n t   i n d e x ,   i n t   n ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   v a l u e   t [ n ] ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x .   T h e   a c c e s s   i s   r a w ;   t h a t   i s ,   i t   d o e s   
 
 n o t   i n v o k e   m e t a m e t h o d s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r a w s e t 
 
 [ - 2 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a _ r a w s e t   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 S i m i l a r   t o   l u a _ s e t t a b l e ,   b u t   d o e s   a   r a w   a s s i g n m e n t   ( i . e . ,   w i t h o u t   m e t a m e t h o d s ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r a w s e t i 
 
 [ - 1 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a _ r a w s e t i   ( l u a _ S t a t e   * L ,   i n t   i n d e x ,   i n t   n ) ; 
 
 D o e s   t h e   e q u i v a l e n t   o f   t [ n ]   =   v ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x   a n d   v   i s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   
 
 s t a c k .   
 
 
 
 T h i s   f u n c t i o n   p o p s   t h e   v a l u e   f r o m   t h e   s t a c k .   T h e   a s s i g n m e n t   i s   r a w ;   t h a t   i s ,   i t   d o e s   n o t   i n v o k e   m e t a m e t h o d s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ R e a d e r 
 
 t y p e d e f   c o n s t   c h a r   *   ( * l u a _ R e a d e r )   ( l u a _ S t a t e   * L , 
 
                                                                         v o i d   * d a t a , 
 
                                                                         s i z e _ t   * s i z e ) ; 
 
 T h e   r e a d e r   f u n c t i o n   u s e d   b y   l u a _ l o a d .   E v e r y   t i m e   i t   n e e d s   a n o t h e r   p i e c e   o f   t h e   c h u n k ,   l u a _ l o a d   c a l l s   t h e   r e a d e r ,   p a s s i n g   
 
 a l o n g   i t s   d a t a   p a r a m e t e r .   T h e   r e a d e r   m u s t   r e t u r n   a   p o i n t e r   t o   a   b l o c k   o f   m e m o r y   w i t h   a   n e w   p i e c e   o f   t h e   c h u n k   a n d   s e t   s i z e   
 
 t o   t h e   b l o c k   s i z e .   T h e   b l o c k   m u s t   e x i s t   u n t i l   t h e   r e a d e r   f u n c t i o n   i s   c a l l e d   a g a i n .   T o   s i g n a l   t h e   e n d   o f   t h e   c h u n k ,   t h e   
 
 r e a d e r   m u s t   r e t u r n   N U L L   o r   s e t   s i z e   t o   z e r o .   T h e   r e a d e r   f u n c t i o n   m a y   r e t u r n   p i e c e s   o f   a n y   s i z e   g r e a t e r   t h a n   z e r o .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r e g i s t e r 
 
 [ - 0 ,   + 0 ,   e ]   
 
 
 
 v o i d   l u a _ r e g i s t e r   ( l u a _ S t a t e   * L , 
 
                                       c o n s t   c h a r   * n a m e , 
 
                                       l u a _ C F u n c t i o n   f ) ; 
 
 S e t s   t h e   C   f u n c t i o n   f   a s   t h e   n e w   v a l u e   o f   g l o b a l   n a m e .   I t   i s   d e f i n e d   a s   a   m a c r o :   
 
 
 
           # d e f i n e   l u a _ r e g i s t e r ( L , n , f )   \ 
 
                         ( l u a _ p u s h c f u n c t i o n ( L ,   f ) ,   l u a _ s e t g l o b a l ( L ,   n ) ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r e m o v e 
 
 [ - 1 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a _ r e m o v e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e m o v e s   t h e   e l e m e n t   a t   t h e   g i v e n   v a l i d   i n d e x ,   s h i f t i n g   d o w n   t h e   e l e m e n t s   a b o v e   t h i s   i n d e x   t o   f i l l   t h e   g a p .   C a n n o t   b e   c a l l e d   
 
 w i t h   a   p s e u d o - i n d e x ,   b e c a u s e   a   p s e u d o - i n d e x   i s   n o t   a n   a c t u a l   s t a c k   p o s i t i o n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r e p l a c e 
 
 [ - 1 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a _ r e p l a c e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 M o v e s   t h e   t o p   e l e m e n t   i n t o   t h e   g i v e n   p o s i t i o n   ( a n d   p o p s   i t ) ,   w i t h o u t   s h i f t i n g   a n y   e l e m e n t   ( t h e r e f o r e   r e p l a c i n g   t h e   v a l u e   a t   
 
 t h e   g i v e n   p o s i t i o n ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ r e s u m e 
 
 [ - ? ,   + ? ,   - ]   
 
 
 
 i n t   l u a _ r e s u m e   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 S t a r t s   a n d   r e s u m e s   a   c o r o u t i n e   i n   a   g i v e n   t h r e a d .   
 
 
 
 T o   s t a r t   a   c o r o u t i n e ,   y o u   f i r s t   c r e a t e   a   n e w   t h r e a d   ( s e e   l u a _ n e w t h r e a d ) ;   t h e n   y o u   p u s h   o n t o   i t s   s t a c k   t h e   m a i n   f u n c t i o n   
 
 p l u s   a n y   a r g u m e n t s ;   t h e n   y o u   c a l l   l u a _ r e s u m e ,   w i t h   n a r g   b e i n g   t h e   n u m b e r   o f   a r g u m e n t s .   T h i s   c a l l   r e t u r n s   w h e n   t h e   c o r o u t i n e   
 
 s u s p e n d s   o r   f i n i s h e s   i t s   e x e c u t i o n .   W h e n   i t   r e t u r n s ,   t h e   s t a c k   c o n t a i n s   a l l   v a l u e s   p a s s e d   t o   l u a _ y i e l d ,   o r   a l l   v a l u e s   
 
 r e t u r n e d   b y   t h e   b o d y   f u n c t i o n .   l u a _ r e s u m e   r e t u r n s   L U A _ Y I E L D   i f   t h e   c o r o u t i n e   y i e l d s ,   0   i f   t h e   c o r o u t i n e   f i n i s h e s   i t s   
 
 e x e c u t i o n   w i t h o u t   e r r o r s ,   o r   a n   e r r o r   c o d e   i n   c a s e   o f   e r r o r s   ( s e e   l u a _ p c a l l ) .   I n   c a s e   o f   e r r o r s ,   t h e   s t a c k   i s   n o t   u n w o u n d ,   
 
 s o   y o u   c a n   u s e   t h e   d e b u g   A P I   o v e r   i t .   T h e   e r r o r   m e s s a g e   i s   o n   t h e   t o p   o f   t h e   s t a c k .   T o   r e s t a r t   a   c o r o u t i n e ,   y o u   p u t   o n   i t s   
 
 s t a c k   o n l y   t h e   v a l u e s   t o   b e   p a s s e d   a s   r e s u l t s   f r o m   y i e l d ,   a n d   t h e n   c a l l   l u a _ r e s u m e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t a l l o c f 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a _ s e t a l l o c f   ( l u a _ S t a t e   * L ,   l u a _ A l l o c   f ,   v o i d   * u d ) ; 
 
 C h a n g e s   t h e   a l l o c a t o r   f u n c t i o n   o f   a   g i v e n   s t a t e   t o   f   w i t h   u s e r   d a t a   u d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t f e n v 
 
 [ - 1 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ s e t f e n v   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P o p s   a   t a b l e   f r o m   t h e   s t a c k   a n d   s e t s   i t   a s   t h e   n e w   e n v i r o n m e n t   f o r   t h e   v a l u e   a t   t h e   g i v e n   i n d e x .   I f   t h e   v a l u e   a t   t h e   g i v e n   
 
 i n d e x   i s   n e i t h e r   a   f u n c t i o n   n o r   a   t h r e a d   n o r   a   u s e r d a t a ,   l u a _ s e t f e n v   r e t u r n s   0 .   O t h e r w i s e   i t   r e t u r n s   1 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t f i e l d 
 
 [ - 1 ,   + 0 ,   e ]   
 
 
 
 v o i d   l u a _ s e t f i e l d   ( l u a _ S t a t e   * L ,   i n t   i n d e x ,   c o n s t   c h a r   * k ) ; 
 
 D o e s   t h e   e q u i v a l e n t   t o   t [ k ]   =   v ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x   a n d   v   i s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 T h i s   f u n c t i o n   p o p s   t h e   v a l u e   f r o m   t h e   s t a c k .   A s   i n   L u a ,   t h i s   f u n c t i o n   m a y   t r i g g e r   a   m e t a m e t h o d   f o r   t h e   " n e w i n d e x "   e v e n t   
 
 ( s e e    2 . 8 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t g l o b a l 
 
 [ - 1 ,   + 0 ,   e ]   
 
 
 
 v o i d   l u a _ s e t g l o b a l   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * n a m e ) ; 
 
 P o p s   a   v a l u e   f r o m   t h e   s t a c k   a n d   s e t s   i t   a s   t h e   n e w   v a l u e   o f   g l o b a l   n a m e .   I t   i s   d e f i n e d   a s   a   m a c r o :   
 
 
 
           # d e f i n e   l u a _ s e t g l o b a l ( L , s )       l u a _ s e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   s ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t m e t a t a b l e 
 
 [ - 1 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ s e t m e t a t a b l e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 P o p s   a   t a b l e   f r o m   t h e   s t a c k   a n d   s e t s   i t   a s   t h e   n e w   m e t a t a b l e   f o r   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t t a b l e 
 
 [ - 2 ,   + 0 ,   e ]   
 
 
 
 v o i d   l u a _ s e t t a b l e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 D o e s   t h e   e q u i v a l e n t   t o   t [ k ]   =   v ,   w h e r e   t   i s   t h e   v a l u e   a t   t h e   g i v e n   v a l i d   i n d e x ,   v   i s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   s t a c k ,   a n d   
 
 k   i s   t h e   v a l u e   j u s t   b e l o w   t h e   t o p .   
 
 
 
 T h i s   f u n c t i o n   p o p s   b o t h   t h e   k e y   a n d   t h e   v a l u e   f r o m   t h e   s t a c k .   A s   i n   L u a ,   t h i s   f u n c t i o n   m a y   t r i g g e r   a   m e t a m e t h o d   f o r   t h e   
 
 " n e w i n d e x "   e v e n t   ( s e e    2 . 8 ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t t o p 
 
 [ - ? ,   + ? ,   - ]   
 
 
 
 v o i d   l u a _ s e t t o p   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 A c c e p t s   a n y   a c c e p t a b l e   i n d e x ,   o r   0 ,   a n d   s e t s   t h e   s t a c k   t o p   t o   t h i s   i n d e x .   I f   t h e   n e w   t o p   i s   l a r g e r   t h a n   t h e   o l d   o n e ,   t h e n   
 
 t h e   n e w   e l e m e n t s   a r e   f i l l e d   w i t h   n i l .   I f   i n d e x   i s   0 ,   t h e n   a l l   s t a c k   e l e m e n t s   a r e   r e m o v e d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ S t a t e 
 
 t y p e d e f   s t r u c t   l u a _ S t a t e   l u a _ S t a t e ; 
 
 O p a q u e   s t r u c t u r e   t h a t   k e e p s   t h e   w h o l e   s t a t e   o f   a   L u a   i n t e r p r e t e r .   T h e   L u a   l i b r a r y   i s   f u l l y   r e e n t r a n t :   i t   h a s   n o   g l o b a l   
 
 v a r i a b l e s .   A l l   i n f o r m a t i o n   a b o u t   a   s t a t e   i s   k e p t   i n   t h i s   s t r u c t u r e .   
 
 
 
 A   p o i n t e r   t o   t h i s   s t a t e   m u s t   b e   p a s s e d   a s   t h e   f i r s t   a r g u m e n t   t o   e v e r y   f u n c t i o n   i n   t h e   l i b r a r y ,   e x c e p t   t o   l u a _ n e w s t a t e ,   
 
 w h i c h   c r e a t e s   a   L u a   s t a t e   f r o m   s c r a t c h .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s t a t u s 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ s t a t u s   ( l u a _ S t a t e   * L ) ; 
 
 R e t u r n s   t h e   s t a t u s   o f   t h e   t h r e a d   L .   
 
 
 
 T h e   s t a t u s   c a n   b e   0   f o r   a   n o r m a l   t h r e a d ,   a n   e r r o r   c o d e   i f   t h e   t h r e a d   f i n i s h e d   i t s   e x e c u t i o n   w i t h   a n   e r r o r ,   o r   L U A _ Y I E L D   i f   
 
 t h e   t h r e a d   i s   s u s p e n d e d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o b o o l e a n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ t o b o o l e a n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   t h e   L u a   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   a   C   b o o l e a n   v a l u e   ( 0   o r   1 ) .   L i k e   a l l   t e s t s   i n   L u a ,   l u a _ t o b o o l e a n   
 
 r e t u r n s   1   f o r   a n y   L u a   v a l u e   d i f f e r e n t   f r o m   f a l s e   a n d   n i l ;   o t h e r w i s e   i t   r e t u r n s   0 .   I t   a l s o   r e t u r n s   0   w h e n   c a l l e d   w i t h   a   
 
 n o n - v a l i d   i n d e x .   ( I f   y o u   w a n t   t o   a c c e p t   o n l y   a c t u a l   b o o l e a n   v a l u e s ,   u s e   l u a _ i s b o o l e a n   t o   t e s t   t h e   v a l u e ' s   t y p e . )   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o c f u n c t i o n 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ C F u n c t i o n   l u a _ t o c f u n c t i o n   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   a   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   a   C   f u n c t i o n .   T h a t   v a l u e   m u s t   b e   a   C   f u n c t i o n ;   o t h e r w i s e ,   r e t u r n s   N U L L .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o i n t e g e r 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ I n t e g e r   l u a _ t o i n t e g e r   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   t h e   L u a   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   t h e   s i g n e d   i n t e g r a l   t y p e   l u a _ I n t e g e r .   T h e   L u a   v a l u e   m u s t   b e   a   
 
 n u m b e r   o r   a   s t r i n g   c o n v e r t i b l e   t o   a   n u m b e r   ( s e e    2 . 2 . 1 ) ;   o t h e r w i s e ,   l u a _ t o i n t e g e r   r e t u r n s   0 .   
 
 
 
 I f   t h e   n u m b e r   i s   n o t   a n   i n t e g e r ,   i t   i s   t r u n c a t e d   i n   s o m e   n o n - s p e c i f i e d   w a y .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o l s t r i n g 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 c o n s t   c h a r   * l u a _ t o l s t r i n g   ( l u a _ S t a t e   * L ,   i n t   i n d e x ,   s i z e _ t   * l e n ) ; 
 
 C o n v e r t s   t h e   L u a   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   a   C   s t r i n g .   I f   l e n   i s   n o t   N U L L ,   i t   a l s o   s e t s   * l e n   w i t h   t h e   s t r i n g   
 
 l e n g t h .   T h e   L u a   v a l u e   m u s t   b e   a   s t r i n g   o r   a   n u m b e r ;   o t h e r w i s e ,   t h e   f u n c t i o n   r e t u r n s   N U L L .   I f   t h e   v a l u e   i s   a   n u m b e r ,   t h e n   
 
 l u a _ t o l s t r i n g   a l s o   c h a n g e s   t h e   a c t u a l   v a l u e   i n   t h e   s t a c k   t o   a   s t r i n g .   ( T h i s   c h a n g e   c o n f u s e s   l u a _ n e x t   w h e n   l u a _ t o l s t r i n g   i s   
 
 a p p l i e d   t o   k e y s   d u r i n g   a   t a b l e   t r a v e r s a l . )   
 
 
 
 l u a _ t o l s t r i n g   r e t u r n s   a   f u l l y   a l i g n e d   p o i n t e r   t o   a   s t r i n g   i n s i d e   t h e   L u a   s t a t e .   T h i s   s t r i n g   a l w a y s   h a s   a   z e r o   ( ' \ 0 ' )   a f t e r   
 
 i t s   l a s t   c h a r a c t e r   ( a s   i n   C ) ,   b u t   c a n   c o n t a i n   o t h e r   z e r o s   i n   i t s   b o d y .   B e c a u s e   L u a   h a s   g a r b a g e   c o l l e c t i o n ,   t h e r e   i s   n o   
 
 g u a r a n t e e   t h a t   t h e   p o i n t e r   r e t u r n e d   b y   l u a _ t o l s t r i n g   w i l l   b e   v a l i d   a f t e r   t h e   c o r r e s p o n d i n g   v a l u e   i s   r e m o v e d   f r o m   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o n u m b e r 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ N u m b e r   l u a _ t o n u m b e r   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   t h e   L u a   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   t h e   C   t y p e   l u a _ N u m b e r   ( s e e   l u a _ N u m b e r ) .   T h e   L u a   v a l u e   m u s t   b e   a   
 
 n u m b e r   o r   a   s t r i n g   c o n v e r t i b l e   t o   a   n u m b e r   ( s e e    2 . 2 . 1 ) ;   o t h e r w i s e ,   l u a _ t o n u m b e r   r e t u r n s   0 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o p o i n t e r 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 c o n s t   v o i d   * l u a _ t o p o i n t e r   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   a   g e n e r i c   C   p o i n t e r   ( v o i d * ) .   T h e   v a l u e   c a n   b e   a   u s e r d a t a ,   a   t a b l e ,   a   
 
 t h r e a d ,   o r   a   f u n c t i o n ;   o t h e r w i s e ,   l u a _ t o p o i n t e r   r e t u r n s   N U L L .   D i f f e r e n t   o b j e c t s   w i l l   g i v e   d i f f e r e n t   p o i n t e r s .   T h e r e   i s   n o   
 
 w a y   t o   c o n v e r t   t h e   p o i n t e r   b a c k   t o   i t s   o r i g i n a l   v a l u e .   
 
 
 
 T y p i c a l l y   t h i s   f u n c t i o n   i s   u s e d   o n l y   f o r   d e b u g   i n f o r m a t i o n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o s t r i n g 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 c o n s t   c h a r   * l u a _ t o s t r i n g   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 E q u i v a l e n t   t o   l u a _ t o l s t r i n g   w i t h   l e n   e q u a l   t o   N U L L .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o t h r e a d 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ S t a t e   * l u a _ t o t h r e a d   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 C o n v e r t s   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   t o   a   L u a   t h r e a d   ( r e p r e s e n t e d   a s   l u a _ S t a t e * ) .   T h i s   v a l u e   m u s t   b e   a   t h r e a d ;   
 
 o t h e r w i s e ,   t h e   f u n c t i o n   r e t u r n s   N U L L .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t o u s e r d a t a 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 v o i d   * l u a _ t o u s e r d a t a   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 I f   t h e   v a l u e   a t   t h e   g i v e n   a c c e p t a b l e   i n d e x   i s   a   f u l l   u s e r d a t a ,   r e t u r n s   i t s   b l o c k   a d d r e s s .   I f   t h e   v a l u e   i s   a   l i g h t   u s e r d a t a ,   
 
 r e t u r n s   i t s   p o i n t e r .   O t h e r w i s e ,   r e t u r n s   N U L L .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t y p e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ t y p e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   t h e   t y p e   o f   t h e   v a l u e   i n   t h e   g i v e n   a c c e p t a b l e   i n d e x ,   o r   L U A _ T N O N E   f o r   a   n o n - v a l i d   i n d e x   ( t h a t   i s ,   a n   i n d e x   t o   a n   
 
 " e m p t y "   s t a c k   p o s i t i o n ) .   T h e   t y p e s   r e t u r n e d   b y   l u a _ t y p e   a r e   c o d e d   b y   t h e   f o l l o w i n g   c o n s t a n t s   d e f i n e d   i n   l u a . h :   L U A _ T N I L ,   
 
 L U A _ T N U M B E R ,   L U A _ T B O O L E A N ,   L U A _ T S T R I N G ,   L U A _ T T A B L E ,   L U A _ T F U N C T I O N ,   L U A _ T U S E R D A T A ,   L U A _ T T H R E A D ,   a n d   L U A _ T L I G H T U S E R D A T A .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ t y p e n a m e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a _ t y p e n a m e     ( l u a _ S t a t e   * L ,   i n t   t p ) ; 
 
 R e t u r n s   t h e   n a m e   o f   t h e   t y p e   e n c o d e d   b y   t h e   v a l u e   t p ,   w h i c h   m u s t   b e   o n e   t h e   v a l u e s   r e t u r n e d   b y   l u a _ t y p e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ W r i t e r 
 
 t y p e d e f   i n t   ( * l u a _ W r i t e r )   ( l u a _ S t a t e   * L , 
 
                                                       c o n s t   v o i d *   p , 
 
                                                       s i z e _ t   s z , 
 
                                                       v o i d *   u d ) ; 
 
 T h e   t y p e   o f   t h e   w r i t e r   f u n c t i o n   u s e d   b y   l u a _ d u m p .   E v e r y   t i m e   i t   p r o d u c e s   a n o t h e r   p i e c e   o f   c h u n k ,   l u a _ d u m p   c a l l s   t h e   w r i t e r ,   
 
 p a s s i n g   a l o n g   t h e   b u f f e r   t o   b e   w r i t t e n   ( p ) ,   i t s   s i z e   ( s z ) ,   a n d   t h e   d a t a   p a r a m e t e r   s u p p l i e d   t o   l u a _ d u m p .   
 
 
 
 T h e   w r i t e r   r e t u r n s   a n   e r r o r   c o d e :   0   m e a n s   n o   e r r o r s ;   a n y   o t h e r   v a l u e   m e a n s   a n   e r r o r   a n d   s t o p s   l u a _ d u m p   f r o m   c a l l i n g   t h e   
 
 w r i t e r   a g a i n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ x m o v e 
 
 [ - ? ,   + ? ,   - ]   
 
 
 
 v o i d   l u a _ x m o v e   ( l u a _ S t a t e   * f r o m ,   l u a _ S t a t e   * t o ,   i n t   n ) ; 
 
 E x c h a n g e   v a l u e s   b e t w e e n   d i f f e r e n t   t h r e a d s   o f   t h e   s a m e   g l o b a l   s t a t e .   
 
 
 
 T h i s   f u n c t i o n   p o p s   n   v a l u e s   f r o m   t h e   s t a c k   f r o m ,   a n d   p u s h e s   t h e m   o n t o   t h e   s t a c k   t o .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ y i e l d 
 
 [ - ? ,   + ? ,   - ]   
 
 
 
 i n t   l u a _ y i e l d     ( l u a _ S t a t e   * L ,   i n t   n r e s u l t s ) ; 
 
 Y i e l d s   a   c o r o u t i n e .   
 
 
 
 T h i s   f u n c t i o n   s h o u l d   o n l y   b e   c a l l e d   a s   t h e   r e t u r n   e x p r e s s i o n   o f   a   C   f u n c t i o n ,   a s   f o l l o w s :   
 
 
 
           r e t u r n   l u a _ y i e l d   ( L ,   n r e s u l t s ) ; 
 
 
 
 W h e n   a   C   f u n c t i o n   c a l l s   l u a _ y i e l d   i n   t h a t   w a y ,   t h e   r u n n i n g   c o r o u t i n e   s u s p e n d s   i t s   e x e c u t i o n ,   a n d   t h e   c a l l   t o   l u a _ r e s u m e   
 
 t h a t   s t a r t e d   t h i s   c o r o u t i n e   r e t u r n s .   T h e   p a r a m e t e r   n r e s u l t s   i s   t h e   n u m b e r   o f   v a l u e s   f r o m   t h e   s t a c k   t h a t   a r e   p a s s e d   a s   
 
 r e s u l t s   t o   l u a _ r e s u m e .   
 
 
 
 3 . 8   -   T h e   D e b u g   I n t e r f a c e 
 
 L u a   h a s   n o   b u i l t - i n   d e b u g g i n g   f a c i l i t i e s .   I n s t e a d ,   i t   o f f e r s   a   s p e c i a l   i n t e r f a c e   b y   m e a n s   o f   f u n c t i o n s   a n d   h o o k s .   T h i s   
 
 i n t e r f a c e   a l l o w s   t h e   c o n s t r u c t i o n   o f   d i f f e r e n t   k i n d s   o f   d e b u g g e r s ,   p r o f i l e r s ,   a n d   o t h e r   t o o l s   t h a t   n e e d   " i n s i d e   i n f o r m a t i o n "   
 
 f r o m   t h e   i n t e r p r e t e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ D e b u g 
 
 t y p e d e f   s t r u c t   l u a _ D e b u g   { 
 
     i n t   e v e n t ; 
 
     c o n s t   c h a r   * n a m e ;                       / *   ( n )   * / 
 
     c o n s t   c h a r   * n a m e w h a t ;               / *   ( n )   * / 
 
     c o n s t   c h a r   * w h a t ;                       / *   ( S )   * / 
 
     c o n s t   c h a r   * s o u r c e ;                   / *   ( S )   * / 
 
     i n t   c u r r e n t l i n e ;                         / *   ( l )   * / 
 
     i n t   n u p s ;                                       / *   ( u )   n u m b e r   o f   u p v a l u e s   * / 
 
     i n t   l i n e d e f i n e d ;                         / *   ( S )   * / 
 
     i n t   l a s t l i n e d e f i n e d ;                 / *   ( S )   * / 
 
     c h a r   s h o r t _ s r c [ L U A _ I D S I Z E ] ;   / *   ( S )   * / 
 
     / *   p r i v a t e   p a r t   * / 
 
     o t h e r   f i e l d s 
 
 }   l u a _ D e b u g ; 
 
 A   s t r u c t u r e   u s e d   t o   c a r r y   d i f f e r e n t   p i e c e s   o f   i n f o r m a t i o n   a b o u t   a n   a c t i v e   f u n c t i o n .   l u a _ g e t s t a c k   f i l l s   o n l y   t h e   p r i v a t e   
 
 p a r t   o f   t h i s   s t r u c t u r e ,   f o r   l a t e r   u s e .   T o   f i l l   t h e   o t h e r   f i e l d s   o f   l u a _ D e b u g   w i t h   u s e f u l   i n f o r m a t i o n ,   c a l l   l u a _ g e t i n f o .   
 
 
 
 T h e   f i e l d s   o f   l u a _ D e b u g   h a v e   t h e   f o l l o w i n g   m e a n i n g :   
 
 
 
 s o u r c e :   I f   t h e   f u n c t i o n   w a s   d e f i n e d   i n   a   s t r i n g ,   t h e n   s o u r c e   i s   t h a t   s t r i n g .   I f   t h e   f u n c t i o n   w a s   d e f i n e d   i n   a   f i l e ,   t h e n   
 
 s o u r c e   s t a r t s   w i t h   a   ' @ '   f o l l o w e d   b y   t h e   f i l e   n a m e .   
 
 s h o r t _ s r c :   a   " p r i n t a b l e "   v e r s i o n   o f   s o u r c e ,   t o   b e   u s e d   i n   e r r o r   m e s s a g e s .   
 
 l i n e d e f i n e d :   t h e   l i n e   n u m b e r   w h e r e   t h e   d e f i n i t i o n   o f   t h e   f u n c t i o n   s t a r t s .   
 
 l a s t l i n e d e f i n e d :   t h e   l i n e   n u m b e r   w h e r e   t h e   d e f i n i t i o n   o f   t h e   f u n c t i o n   e n d s .   
 
 w h a t :   t h e   s t r i n g   " L u a "   i f   t h e   f u n c t i o n   i s   a   L u a   f u n c t i o n ,   " C "   i f   i t   i s   a   C   f u n c t i o n ,   " m a i n "   i f   i t   i s   t h e   m a i n   p a r t   o f   a   
 
 c h u n k ,   a n d   " t a i l "   i f   i t   w a s   a   f u n c t i o n   t h a t   d i d   a   t a i l   c a l l .   I n   t h e   l a t t e r   c a s e ,   L u a   h a s   n o   o t h e r   i n f o r m a t i o n   a b o u t   t h e   
 
 f u n c t i o n .   
 
 c u r r e n t l i n e :   t h e   c u r r e n t   l i n e   w h e r e   t h e   g i v e n   f u n c t i o n   i s   e x e c u t i n g .   W h e n   n o   l i n e   i n f o r m a t i o n   i s   a v a i l a b l e ,   c u r r e n t l i n e   i s   
 
 s e t   t o   - 1 .   
 
 n a m e :   a   r e a s o n a b l e   n a m e   f o r   t h e   g i v e n   f u n c t i o n .   B e c a u s e   f u n c t i o n s   i n   L u a   a r e   f i r s t - c l a s s   v a l u e s ,   t h e y   d o   n o t   h a v e   a   f i x e d   
 
 n a m e :   s o m e   f u n c t i o n s   c a n   b e   t h e   v a l u e   o f   m u l t i p l e   g l o b a l   v a r i a b l e s ,   w h i l e   o t h e r s   c a n   b e   s t o r e d   o n l y   i n   a   t a b l e   f i e l d .   T h e   
 
 l u a _ g e t i n f o   f u n c t i o n   c h e c k s   h o w   t h e   f u n c t i o n   w a s   c a l l e d   t o   f i n d   a   s u i t a b l e   n a m e .   I f   i t   c a n n o t   f i n d   a   n a m e ,   t h e n   n a m e   i s   s e t   
 
 t o   N U L L .   
 
 n a m e w h a t :   e x p l a i n s   t h e   n a m e   f i e l d .   T h e   v a l u e   o f   n a m e w h a t   c a n   b e   " g l o b a l " ,   " l o c a l " ,   " m e t h o d " ,   " f i e l d " ,   " u p v a l u e " ,   o r   " "   ( t h e   
 
 e m p t y   s t r i n g ) ,   a c c o r d i n g   t o   h o w   t h e   f u n c t i o n   w a s   c a l l e d .   ( L u a   u s e s   t h e   e m p t y   s t r i n g   w h e n   n o   o t h e r   o p t i o n   s e e m s   t o   a p p l y . )   
 
 n u p s :   t h e   n u m b e r   o f   u p v a l u e s   o f   t h e   f u n c t i o n .   
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t h o o k 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ H o o k   l u a _ g e t h o o k   ( l u a _ S t a t e   * L ) ; 
 
 R e t u r n s   t h e   c u r r e n t   h o o k   f u n c t i o n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t h o o k c o u n t 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ g e t h o o k c o u n t   ( l u a _ S t a t e   * L ) ; 
 
 R e t u r n s   t h e   c u r r e n t   h o o k   c o u n t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t h o o k m a s k 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ g e t h o o k m a s k   ( l u a _ S t a t e   * L ) ; 
 
 R e t u r n s   t h e   c u r r e n t   h o o k   m a s k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t i n f o 
 
 [ - ( 0 | 1 ) ,   + ( 0 | 1 | 2 ) ,   m ]   
 
 
 
 i n t   l u a _ g e t i n f o   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * w h a t ,   l u a _ D e b u g   * a r ) ; 
 
 R e t u r n s   i n f o r m a t i o n   a b o u t   a   s p e c i f i c   f u n c t i o n   o r   f u n c t i o n   i n v o c a t i o n .   
 
 
 
 T o   g e t   i n f o r m a t i o n   a b o u t   a   f u n c t i o n   i n v o c a t i o n ,   t h e   p a r a m e t e r   a r   m u s t   b e   a   v a l i d   a c t i v a t i o n   r e c o r d   t h a t   w a s   f i l l e d   b y   a   
 
 p r e v i o u s   c a l l   t o   l u a _ g e t s t a c k   o r   g i v e n   a s   a r g u m e n t   t o   a   h o o k   ( s e e   l u a _ H o o k ) .   
 
 
 
 T o   g e t   i n f o r m a t i o n   a b o u t   a   f u n c t i o n   y o u   p u s h   i t   o n t o   t h e   s t a c k   a n d   s t a r t   t h e   w h a t   s t r i n g   w i t h   t h e   c h a r a c t e r   ' > ' .   ( I n   t h a t   
 
 c a s e ,   l u a _ g e t i n f o   p o p s   t h e   f u n c t i o n   i n   t h e   t o p   o f   t h e   s t a c k . )   F o r   i n s t a n c e ,   t o   k n o w   i n   w h i c h   l i n e   a   f u n c t i o n   f   w a s   d e f i n e d ,   
 
 y o u   c a n   w r i t e   t h e   f o l l o w i n g   c o d e :   
 
 
 
           l u a _ D e b u g   a r ; 
 
           l u a _ g e t f i e l d ( L ,   L U A _ G L O B A L S I N D E X ,   " f " ) ;     / *   g e t   g l o b a l   ' f '   * / 
 
           l u a _ g e t i n f o ( L ,   " > S " ,   & a r ) ; 
 
           p r i n t f ( " % d \ n " ,   a r . l i n e d e f i n e d ) ; 
 
 
 
 E a c h   c h a r a c t e r   i n   t h e   s t r i n g   w h a t   s e l e c t s   s o m e   f i e l d s   o f   t h e   s t r u c t u r e   a r   t o   b e   f i l l e d   o r   a   v a l u e   t o   b e   p u s h e d   o n   t h e   
 
 s t a c k :   
 
 
 
 ' n ' :   f i l l s   i n   t h e   f i e l d   n a m e   a n d   n a m e w h a t ;   
 
 ' S ' :   f i l l s   i n   t h e   f i e l d s   s o u r c e ,   s h o r t _ s r c ,   l i n e d e f i n e d ,   l a s t l i n e d e f i n e d ,   a n d   w h a t ;   
 
 ' l ' :   f i l l s   i n   t h e   f i e l d   c u r r e n t l i n e ;   
 
 ' u ' :   f i l l s   i n   t h e   f i e l d   n u p s ;   
 
 ' f ' :   p u s h e s   o n t o   t h e   s t a c k   t h e   f u n c t i o n   t h a t   i s   r u n n i n g   a t   t h e   g i v e n   l e v e l ;   
 
 ' L ' :   p u s h e s   o n t o   t h e   s t a c k   a   t a b l e   w h o s e   i n d i c e s   a r e   t h e   n u m b e r s   o f   t h e   l i n e s   t h a t   a r e   v a l i d   o n   t h e   f u n c t i o n .   ( A   v a l i d   l i n e   
 
 i s   a   l i n e   w i t h   s o m e   a s s o c i a t e d   c o d e ,   t h a t   i s ,   a   l i n e   w h e r e   y o u   c a n   p u t   a   b r e a k   p o i n t .   N o n - v a l i d   l i n e s   i n c l u d e   e m p t y   l i n e s   
 
 a n d   c o m m e n t s . )   
 
 T h i s   f u n c t i o n   r e t u r n s   0   o n   e r r o r   ( f o r   i n s t a n c e ,   a n   i n v a l i d   o p t i o n   i n   w h a t ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t l o c a l 
 
 [ - 0 ,   + ( 0 | 1 ) ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a _ g e t l o c a l   ( l u a _ S t a t e   * L ,   l u a _ D e b u g   * a r ,   i n t   n ) ; 
 
 G e t s   i n f o r m a t i o n   a b o u t   a   l o c a l   v a r i a b l e   o f   a   g i v e n   a c t i v a t i o n   r e c o r d .   T h e   p a r a m e t e r   a r   m u s t   b e   a   v a l i d   a c t i v a t i o n   r e c o r d   
 
 t h a t   w a s   f i l l e d   b y   a   p r e v i o u s   c a l l   t o   l u a _ g e t s t a c k   o r   g i v e n   a s   a r g u m e n t   t o   a   h o o k   ( s e e   l u a _ H o o k ) .   T h e   i n d e x   n   s e l e c t s   w h i c h   
 
 l o c a l   v a r i a b l e   t o   i n s p e c t   ( 1   i s   t h e   f i r s t   p a r a m e t e r   o r   a c t i v e   l o c a l   v a r i a b l e ,   a n d   s o   o n ,   u n t i l   t h e   l a s t   a c t i v e   l o c a l   
 
 v a r i a b l e ) .   l u a _ g e t l o c a l   p u s h e s   t h e   v a r i a b l e ' s   v a l u e   o n t o   t h e   s t a c k   a n d   r e t u r n s   i t s   n a m e .   
 
 
 
 V a r i a b l e   n a m e s   s t a r t i n g   w i t h   ' ( '   ( o p e n   p a r e n t h e s e s )   r e p r e s e n t   i n t e r n a l   v a r i a b l e s   ( l o o p   c o n t r o l   v a r i a b l e s ,   t e m p o r a r i e s ,   a n d   
 
 C   f u n c t i o n   l o c a l s ) .   
 
 
 
 R e t u r n s   N U L L   ( a n d   p u s h e s   n o t h i n g )   w h e n   t h e   i n d e x   i s   g r e a t e r   t h a n   t h e   n u m b e r   o f   a c t i v e   l o c a l   v a r i a b l e s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t s t a c k 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ g e t s t a c k   ( l u a _ S t a t e   * L ,   i n t   l e v e l ,   l u a _ D e b u g   * a r ) ; 
 
 G e t   i n f o r m a t i o n   a b o u t   t h e   i n t e r p r e t e r   r u n t i m e   s t a c k .   
 
 
 
 T h i s   f u n c t i o n   f i l l s   p a r t s   o f   a   l u a _ D e b u g   s t r u c t u r e   w i t h   a n   i d e n t i f i c a t i o n   o f   t h e   a c t i v a t i o n   r e c o r d   o f   t h e   f u n c t i o n   
 
 e x e c u t i n g   a t   a   g i v e n   l e v e l .   L e v e l   0   i s   t h e   c u r r e n t   r u n n i n g   f u n c t i o n ,   w h e r e a s   l e v e l   n + 1   i s   t h e   f u n c t i o n   t h a t   h a s   c a l l e d   
 
 l e v e l   n .   W h e n   t h e r e   a r e   n o   e r r o r s ,   l u a _ g e t s t a c k   r e t u r n s   1 ;   w h e n   c a l l e d   w i t h   a   l e v e l   g r e a t e r   t h a n   t h e   s t a c k   d e p t h ,   i t   
 
 r e t u r n s   0 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ g e t u p v a l u e 
 
 [ - 0 ,   + ( 0 | 1 ) ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a _ g e t u p v a l u e   ( l u a _ S t a t e   * L ,   i n t   f u n c i n d e x ,   i n t   n ) ; 
 
 G e t s   i n f o r m a t i o n   a b o u t   a   c l o s u r e ' s   u p v a l u e .   ( F o r   L u a   f u n c t i o n s ,   u p v a l u e s   a r e   t h e   e x t e r n a l   l o c a l   v a r i a b l e s   t h a t   t h e   f u n c t i o n   
 
 u s e s ,   a n d   t h a t   a r e   c o n s e q u e n t l y   i n c l u d e d   i n   i t s   c l o s u r e . )   l u a _ g e t u p v a l u e   g e t s   t h e   i n d e x   n   o f   a n   u p v a l u e ,   p u s h e s   t h e   
 
 u p v a l u e ' s   v a l u e   o n t o   t h e   s t a c k ,   a n d   r e t u r n s   i t s   n a m e .   f u n c i n d e x   p o i n t s   t o   t h e   c l o s u r e   i n   t h e   s t a c k .   ( U p v a l u e s   h a v e   n o   
 
 p a r t i c u l a r   o r d e r ,   a s   t h e y   a r e   a c t i v e   t h r o u g h   t h e   w h o l e   f u n c t i o n .   S o ,   t h e y   a r e   n u m b e r e d   i n   a n   a r b i t r a r y   o r d e r . )   
 
 
 
 R e t u r n s   N U L L   ( a n d   p u s h e s   n o t h i n g )   w h e n   t h e   i n d e x   i s   g r e a t e r   t h a n   t h e   n u m b e r   o f   u p v a l u e s .   F o r   C   f u n c t i o n s ,   t h i s   f u n c t i o n   
 
 u s e s   t h e   e m p t y   s t r i n g   " "   a s   a   n a m e   f o r   a l l   u p v a l u e s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ H o o k 
 
 t y p e d e f   v o i d   ( * l u a _ H o o k )   ( l u a _ S t a t e   * L ,   l u a _ D e b u g   * a r ) ; 
 
 T y p e   f o r   d e b u g g i n g   h o o k   f u n c t i o n s .   
 
 
 
 W h e n e v e r   a   h o o k   i s   c a l l e d ,   i t s   a r   a r g u m e n t   h a s   i t s   f i e l d   e v e n t   s e t   t o   t h e   s p e c i f i c   e v e n t   t h a t   t r i g g e r e d   t h e   h o o k .   L u a   
 
 i d e n t i f i e s   t h e s e   e v e n t s   w i t h   t h e   f o l l o w i n g   c o n s t a n t s :   L U A _ H O O K C A L L ,   L U A _ H O O K R E T ,   L U A _ H O O K T A I L R E T ,   L U A _ H O O K L I N E ,   a n d   
 
 L U A _ H O O K C O U N T .   M o r e o v e r ,   f o r   l i n e   e v e n t s ,   t h e   f i e l d   c u r r e n t l i n e   i s   a l s o   s e t .   T o   g e t   t h e   v a l u e   o f   a n y   o t h e r   f i e l d   i n   a r ,   t h e   
 
 h o o k   m u s t   c a l l   l u a _ g e t i n f o .   F o r   r e t u r n   e v e n t s ,   e v e n t   c a n   b e   L U A _ H O O K R E T ,   t h e   n o r m a l   v a l u e ,   o r   L U A _ H O O K T A I L R E T .   I n   t h e   
 
 l a t t e r   c a s e ,   L u a   i s   s i m u l a t i n g   a   r e t u r n   f r o m   a   f u n c t i o n   t h a t   d i d   a   t a i l   c a l l ;   i n   t h i s   c a s e ,   i t   i s   u s e l e s s   t o   c a l l   
 
 l u a _ g e t i n f o .   
 
 
 
 W h i l e   L u a   i s   r u n n i n g   a   h o o k ,   i t   d i s a b l e s   o t h e r   c a l l s   t o   h o o k s .   T h e r e f o r e ,   i f   a   h o o k   c a l l s   b a c k   L u a   t o   e x e c u t e   a   f u n c t i o n   o r   
 
 a   c h u n k ,   t h i s   e x e c u t i o n   o c c u r s   w i t h o u t   a n y   c a l l s   t o   h o o k s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t h o o k 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 i n t   l u a _ s e t h o o k   ( l u a _ S t a t e   * L ,   l u a _ H o o k   f ,   i n t   m a s k ,   i n t   c o u n t ) ; 
 
 S e t s   t h e   d e b u g g i n g   h o o k   f u n c t i o n .   
 
 
 
 A r g u m e n t   f   i s   t h e   h o o k   f u n c t i o n .   m a s k   s p e c i f i e s   o n   w h i c h   e v e n t s   t h e   h o o k   w i l l   b e   c a l l e d :   i t   i s   f o r m e d   b y   a   b i t w i s e   o r   o f   
 
 t h e   c o n s t a n t s   L U A _ M A S K C A L L ,   L U A _ M A S K R E T ,   L U A _ M A S K L I N E ,   a n d   L U A _ M A S K C O U N T .   T h e   c o u n t   a r g u m e n t   i s   o n l y   m e a n i n g f u l   w h e n   t h e   
 
 m a s k   i n c l u d e s   L U A _ M A S K C O U N T .   F o r   e a c h   e v e n t ,   t h e   h o o k   i s   c a l l e d   a s   e x p l a i n e d   b e l o w :   
 
 
 
 T h e   c a l l   h o o k :   i s   c a l l e d   w h e n   t h e   i n t e r p r e t e r   c a l l s   a   f u n c t i o n .   T h e   h o o k   i s   c a l l e d   j u s t   a f t e r   L u a   e n t e r s   t h e   n e w   f u n c t i o n ,   
 
 b e f o r e   t h e   f u n c t i o n   g e t s   i t s   a r g u m e n t s .   
 
 T h e   r e t u r n   h o o k :   i s   c a l l e d   w h e n   t h e   i n t e r p r e t e r   r e t u r n s   f r o m   a   f u n c t i o n .   T h e   h o o k   i s   c a l l e d   j u s t   b e f o r e   L u a   l e a v e s   t h e   
 
 f u n c t i o n .   Y o u   h a v e   n o   a c c e s s   t o   t h e   v a l u e s   t o   b e   r e t u r n e d   b y   t h e   f u n c t i o n .   
 
 T h e   l i n e   h o o k :   i s   c a l l e d   w h e n   t h e   i n t e r p r e t e r   i s   a b o u t   t o   s t a r t   t h e   e x e c u t i o n   o f   a   n e w   l i n e   o f   c o d e ,   o r   w h e n   i t   j u m p s   b a c k   
 
 i n   t h e   c o d e   ( e v e n   t o   t h e   s a m e   l i n e ) .   ( T h i s   e v e n t   o n l y   h a p p e n s   w h i l e   L u a   i s   e x e c u t i n g   a   L u a   f u n c t i o n . )   
 
 T h e   c o u n t   h o o k :   i s   c a l l e d   a f t e r   t h e   i n t e r p r e t e r   e x e c u t e s   e v e r y   c o u n t   i n s t r u c t i o n s .   ( T h i s   e v e n t   o n l y   h a p p e n s   w h i l e   L u a   i s   
 
 e x e c u t i n g   a   L u a   f u n c t i o n . )   
 
 A   h o o k   i s   d i s a b l e d   b y   s e t t i n g   m a s k   t o   z e r o .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t l o c a l 
 
 [ - ( 0 | 1 ) ,   + 0 ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a _ s e t l o c a l   ( l u a _ S t a t e   * L ,   l u a _ D e b u g   * a r ,   i n t   n ) ; 
 
 S e t s   t h e   v a l u e   o f   a   l o c a l   v a r i a b l e   o f   a   g i v e n   a c t i v a t i o n   r e c o r d .   P a r a m e t e r s   a r   a n d   n   a r e   a s   i n   l u a _ g e t l o c a l   ( s e e   
 
 l u a _ g e t l o c a l ) .   l u a _ s e t l o c a l   a s s i g n s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   s t a c k   t o   t h e   v a r i a b l e   a n d   r e t u r n s   i t s   n a m e .   I t   a l s o   p o p s   
 
 t h e   v a l u e   f r o m   t h e   s t a c k .   
 
 
 
 R e t u r n s   N U L L   ( a n d   p o p s   n o t h i n g )   w h e n   t h e   i n d e x   i s   g r e a t e r   t h a n   t h e   n u m b e r   o f   a c t i v e   l o c a l   v a r i a b l e s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a _ s e t u p v a l u e 
 
 [ - ( 0 | 1 ) ,   + 0 ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a _ s e t u p v a l u e   ( l u a _ S t a t e   * L ,   i n t   f u n c i n d e x ,   i n t   n ) ; 
 
 S e t s   t h e   v a l u e   o f   a   c l o s u r e ' s   u p v a l u e .   I t   a s s i g n s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   s t a c k   t o   t h e   u p v a l u e   a n d   r e t u r n s   i t s   n a m e .   I t   
 
 a l s o   p o p s   t h e   v a l u e   f r o m   t h e   s t a c k .   P a r a m e t e r s   f u n c i n d e x   a n d   n   a r e   a s   i n   t h e   l u a _ g e t u p v a l u e   ( s e e   l u a _ g e t u p v a l u e ) .   
 
 
 
 R e t u r n s   N U L L   ( a n d   p o p s   n o t h i n g )   w h e n   t h e   i n d e x   i s   g r e a t e r   t h a n   t h e   n u m b e r   o f   u p v a l u e s .   
 
 
 
 4   -   T h e   A u x i l i a r y   L i b r a r y 
 
 T h e   a u x i l i a r y   l i b r a r y   p r o v i d e s   s e v e r a l   c o n v e n i e n t   f u n c t i o n s   t o   i n t e r f a c e   C   w i t h   L u a .   W h i l e   t h e   b a s i c   A P I   p r o v i d e s   t h e   
 
 p r i m i t i v e   f u n c t i o n s   f o r   a l l   i n t e r a c t i o n s   b e t w e e n   C   a n d   L u a ,   t h e   a u x i l i a r y   l i b r a r y   p r o v i d e s   h i g h e r - l e v e l   f u n c t i o n s   f o r   s o m e   
 
 c o m m o n   t a s k s .   
 
 
 
 A l l   f u n c t i o n s   f r o m   t h e   a u x i l i a r y   l i b r a r y   a r e   d e f i n e d   i n   h e a d e r   f i l e   l a u x l i b . h   a n d   h a v e   a   p r e f i x   l u a L _ .   
 
 
 
 A l l   f u n c t i o n s   i n   t h e   a u x i l i a r y   l i b r a r y   a r e   b u i l t   o n   t o p   o f   t h e   b a s i c   A P I ,   a n d   s o   t h e y   p r o v i d e   n o t h i n g   t h a t   c a n n o t   b e   d o n e   
 
 w i t h   t h i s   A P I .   
 
 
 
 S e v e r a l   f u n c t i o n s   i n   t h e   a u x i l i a r y   l i b r a r y   a r e   u s e d   t o   c h e c k   C   f u n c t i o n   a r g u m e n t s .   T h e i r   n a m e s   a r e   a l w a y s   l u a L _ c h e c k *   o r   
 
 l u a L _ o p t * .   A l l   o f   t h e s e   f u n c t i o n s   t h r o w   a n   e r r o r   i f   t h e   c h e c k   i s   n o t   s a t i s f i e d .   B e c a u s e   t h e   e r r o r   m e s s a g e   i s   f o r m a t t e d   f o r   
 
 a r g u m e n t s   ( e . g . ,   " b a d   a r g u m e n t   # 1 " ) ,   y o u   s h o u l d   n o t   u s e   t h e s e   f u n c t i o n s   f o r   o t h e r   s t a c k   v a l u e s .   
 
 
 
 4 . 1   -   F u n c t i o n s   a n d   T y p e s 
 
 H e r e   w e   l i s t   a l l   f u n c t i o n s   a n d   t y p e s   f r o m   t h e   a u x i l i a r y   l i b r a r y   i n   a l p h a b e t i c a l   o r d e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a d d c h a r 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ a d d c h a r   ( l u a L _ B u f f e r   * B ,   c h a r   c ) ; 
 
 A d d s   t h e   c h a r a c t e r   c   t o   t h e   b u f f e r   B   ( s e e   l u a L _ B u f f e r ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a d d l s t r i n g 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ a d d l s t r i n g   ( l u a L _ B u f f e r   * B ,   c o n s t   c h a r   * s ,   s i z e _ t   l ) ; 
 
 A d d s   t h e   s t r i n g   p o i n t e d   t o   b y   s   w i t h   l e n g t h   l   t o   t h e   b u f f e r   B   ( s e e   l u a L _ B u f f e r ) .   T h e   s t r i n g   m a y   c o n t a i n   e m b e d d e d   z e r o s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a d d s i z e 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ a d d s i z e   ( l u a L _ B u f f e r   * B ,   s i z e _ t   n ) ; 
 
 A d d s   t o   t h e   b u f f e r   B   ( s e e   l u a L _ B u f f e r )   a   s t r i n g   o f   l e n g t h   n   p r e v i o u s l y   c o p i e d   t o   t h e   b u f f e r   a r e a   ( s e e   l u a L _ p r e p b u f f e r ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a d d s t r i n g 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ a d d s t r i n g   ( l u a L _ B u f f e r   * B ,   c o n s t   c h a r   * s ) ; 
 
 A d d s   t h e   z e r o - t e r m i n a t e d   s t r i n g   p o i n t e d   t o   b y   s   t o   t h e   b u f f e r   B   ( s e e   l u a L _ B u f f e r ) .   T h e   s t r i n g   m a y   n o t   c o n t a i n   e m b e d d e d   z e r o s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a d d v a l u e 
 
 [ - 1 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ a d d v a l u e   ( l u a L _ B u f f e r   * B ) ; 
 
 A d d s   t h e   v a l u e   a t   t h e   t o p   o f   t h e   s t a c k   t o   t h e   b u f f e r   B   ( s e e   l u a L _ B u f f e r ) .   P o p s   t h e   v a l u e .   
 
 
 
 T h i s   i s   t h e   o n l y   f u n c t i o n   o n   s t r i n g   b u f f e r s   t h a t   c a n   ( a n d   m u s t )   b e   c a l l e d   w i t h   a n   e x t r a   e l e m e n t   o n   t h e   s t a c k ,   w h i c h   i s   t h e   
 
 v a l u e   t o   b e   a d d e d   t o   t h e   b u f f e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a r g c h e c k 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 v o i d   l u a L _ a r g c h e c k   ( l u a _ S t a t e   * L , 
 
                                         i n t   c o n d , 
 
                                         i n t   n a r g , 
 
                                         c o n s t   c h a r   * e x t r a m s g ) ; 
 
 C h e c k s   w h e t h e r   c o n d   i s   t r u e .   I f   n o t ,   r a i s e s   a n   e r r o r   w i t h   t h e   f o l l o w i n g   m e s s a g e ,   w h e r e   f u n c   i s   r e t r i e v e d   f r o m   t h e   c a l l   
 
 s t a c k :   
 
 
 
           b a d   a r g u m e n t   # < n a r g >   t o   < f u n c >   ( < e x t r a m s g > ) 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ a r g e r r o r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ a r g e r r o r   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   c o n s t   c h a r   * e x t r a m s g ) ; 
 
 R a i s e s   a n   e r r o r   w i t h   t h e   f o l l o w i n g   m e s s a g e ,   w h e r e   f u n c   i s   r e t r i e v e d   f r o m   t h e   c a l l   s t a c k :   
 
 
 
           b a d   a r g u m e n t   # < n a r g >   t o   < f u n c >   ( < e x t r a m s g > ) 
 
 
 
 T h i s   f u n c t i o n   n e v e r   r e t u r n s ,   b u t   i t   i s   a n   i d i o m   t o   u s e   i t   i n   C   f u n c t i o n s   a s   r e t u r n   l u a L _ a r g e r r o r ( a r g s ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ B u f f e r 
 
 t y p e d e f   s t r u c t   l u a L _ B u f f e r   l u a L _ B u f f e r ; 
 
 T y p e   f o r   a   s t r i n g   b u f f e r .   
 
 
 
 A   s t r i n g   b u f f e r   a l l o w s   C   c o d e   t o   b u i l d   L u a   s t r i n g s   p i e c e m e a l .   I t s   p a t t e r n   o f   u s e   i s   a s   f o l l o w s :   
 
 
 
 F i r s t   y o u   d e c l a r e   a   v a r i a b l e   b   o f   t y p e   l u a L _ B u f f e r .   
 
 T h e n   y o u   i n i t i a l i z e   i t   w i t h   a   c a l l   l u a L _ b u f f i n i t ( L ,   & b ) .   
 
 T h e n   y o u   a d d   s t r i n g   p i e c e s   t o   t h e   b u f f e r   c a l l i n g   a n y   o f   t h e   l u a L _ a d d *   f u n c t i o n s .   
 
 Y o u   f i n i s h   b y   c a l l i n g   l u a L _ p u s h r e s u l t ( & b ) .   T h i s   c a l l   l e a v e s   t h e   f i n a l   s t r i n g   o n   t h e   t o p   o f   t h e   s t a c k .   
 
 D u r i n g   i t s   n o r m a l   o p e r a t i o n ,   a   s t r i n g   b u f f e r   u s e s   a   v a r i a b l e   n u m b e r   o f   s t a c k   s l o t s .   S o ,   w h i l e   u s i n g   a   b u f f e r ,   y o u   c a n n o t   
 
 a s s u m e   t h a t   y o u   k n o w   w h e r e   t h e   t o p   o f   t h e   s t a c k   i s .   Y o u   c a n   u s e   t h e   s t a c k   b e t w e e n   s u c c e s s i v e   c a l l s   t o   b u f f e r   o p e r a t i o n s   a s   
 
 l o n g   a s   t h a t   u s e   i s   b a l a n c e d ;   t h a t   i s ,   w h e n   y o u   c a l l   a   b u f f e r   o p e r a t i o n ,   t h e   s t a c k   i s   a t   t h e   s a m e   l e v e l   i t   w a s   i m m e d i a t e l y   
 
 a f t e r   t h e   p r e v i o u s   b u f f e r   o p e r a t i o n .   ( T h e   o n l y   e x c e p t i o n   t o   t h i s   r u l e   i s   l u a L _ a d d v a l u e . )   A f t e r   c a l l i n g   l u a L _ p u s h r e s u l t   t h e   
 
 s t a c k   i s   b a c k   t o   i t s   l e v e l   w h e n   t h e   b u f f e r   w a s   i n i t i a l i z e d ,   p l u s   t h e   f i n a l   s t r i n g   o n   i t s   t o p .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ b u f f i n i t 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a L _ b u f f i n i t   ( l u a _ S t a t e   * L ,   l u a L _ B u f f e r   * B ) ; 
 
 I n i t i a l i z e s   a   b u f f e r   B .   T h i s   f u n c t i o n   d o e s   n o t   a l l o c a t e   a n y   s p a c e ;   t h e   b u f f e r   m u s t   b e   d e c l a r e d   a s   a   v a r i a b l e   ( s e e   
 
 l u a L _ B u f f e r ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c a l l m e t a 
 
 [ - 0 ,   + ( 0 | 1 ) ,   e ]   
 
 
 
 i n t   l u a L _ c a l l m e t a   ( l u a _ S t a t e   * L ,   i n t   o b j ,   c o n s t   c h a r   * e ) ; 
 
 C a l l s   a   m e t a m e t h o d .   
 
 
 
 I f   t h e   o b j e c t   a t   i n d e x   o b j   h a s   a   m e t a t a b l e   a n d   t h i s   m e t a t a b l e   h a s   a   f i e l d   e ,   t h i s   f u n c t i o n   c a l l s   t h i s   f i e l d   a n d   p a s s e s   t h e   
 
 o b j e c t   a s   i t s   o n l y   a r g u m e n t .   I n   t h i s   c a s e   t h i s   f u n c t i o n   r e t u r n s   1   a n d   p u s h e s   o n t o   t h e   s t a c k   t h e   v a l u e   r e t u r n e d   b y   t h e   c a l l .   
 
 I f   t h e r e   i s   n o   m e t a t a b l e   o r   n o   m e t a m e t h o d ,   t h i s   f u n c t i o n   r e t u r n s   0   ( w i t h o u t   p u s h i n g   a n y   v a l u e   o n   t h e   s t a c k ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k a n y 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 v o i d   l u a L _ c h e c k a n y   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   h a s   a n   a r g u m e n t   o f   a n y   t y p e   ( i n c l u d i n g   n i l )   a t   p o s i t i o n   n a r g .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k i n t 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ c h e c k i n t   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r   a n d   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a n   i n t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k i n t e g e r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l u a _ I n t e g e r   l u a L _ c h e c k i n t e g e r   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r   a n d   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a   l u a _ I n t e g e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k l o n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l o n g   l u a L _ c h e c k l o n g   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r   a n d   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a   l o n g .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k l s t r i n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 c o n s t   c h a r   * l u a L _ c h e c k l s t r i n g   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   s i z e _ t   * l ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   s t r i n g   a n d   r e t u r n s   t h i s   s t r i n g ;   i f   l   i s   n o t   N U L L   f i l l s   * l   w i t h   t h e   s t r i n g ' s   
 
 l e n g t h .   
 
 
 
 T h i s   f u n c t i o n   u s e s   l u a _ t o l s t r i n g   t o   g e t   i t s   r e s u l t ,   s o   a l l   c o n v e r s i o n s   a n d   c a v e a t s   o f   t h a t   f u n c t i o n   a p p l y   h e r e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k n u m b e r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l u a _ N u m b e r   l u a L _ c h e c k n u m b e r   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r   a n d   r e t u r n s   t h i s   n u m b e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k o p t i o n 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ c h e c k o p t i o n   ( l u a _ S t a t e   * L , 
 
                                             i n t   n a r g , 
 
                                             c o n s t   c h a r   * d e f , 
 
                                             c o n s t   c h a r   * c o n s t   l s t [ ] ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   s t r i n g   a n d   s e a r c h e s   f o r   t h i s   s t r i n g   i n   t h e   a r r a y   l s t   ( w h i c h   m u s t   b e   
 
 N U L L - t e r m i n a t e d ) .   R e t u r n s   t h e   i n d e x   i n   t h e   a r r a y   w h e r e   t h e   s t r i n g   w a s   f o u n d .   R a i s e s   a n   e r r o r   i f   t h e   a r g u m e n t   i s   n o t   a   
 
 s t r i n g   o r   i f   t h e   s t r i n g   c a n n o t   b e   f o u n d .   
 
 
 
 I f   d e f   i s   n o t   N U L L ,   t h e   f u n c t i o n   u s e s   d e f   a s   a   d e f a u l t   v a l u e   w h e n   t h e r e   i s   n o   a r g u m e n t   n a r g   o r   i f   t h i s   a r g u m e n t   i s   n i l .   
 
 
 
 T h i s   i s   a   u s e f u l   f u n c t i o n   f o r   m a p p i n g   s t r i n g s   t o   C   e n u m s .   ( T h e   u s u a l   c o n v e n t i o n   i n   L u a   l i b r a r i e s   i s   t o   u s e   s t r i n g s   i n s t e a d   
 
 o f   n u m b e r s   t o   s e l e c t   o p t i o n s . )   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k s t a c k 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 v o i d   l u a L _ c h e c k s t a c k   ( l u a _ S t a t e   * L ,   i n t   s z ,   c o n s t   c h a r   * m s g ) ; 
 
 G r o w s   t h e   s t a c k   s i z e   t o   t o p   +   s z   e l e m e n t s ,   r a i s i n g   a n   e r r o r   i f   t h e   s t a c k   c a n n o t   g r o w   t o   t h a t   s i z e .   m s g   i s   a n   a d d i t i o n a l   
 
 t e x t   t o   g o   i n t o   t h e   e r r o r   m e s s a g e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k s t r i n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 c o n s t   c h a r   * l u a L _ c h e c k s t r i n g   ( l u a _ S t a t e   * L ,   i n t   n a r g ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   s t r i n g   a n d   r e t u r n s   t h i s   s t r i n g .   
 
 
 
 T h i s   f u n c t i o n   u s e s   l u a _ t o l s t r i n g   t o   g e t   i t s   r e s u l t ,   s o   a l l   c o n v e r s i o n s   a n d   c a v e a t s   o f   t h a t   f u n c t i o n   a p p l y   h e r e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k t y p e 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 v o i d   l u a L _ c h e c k t y p e   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   i n t   t ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   h a s   t y p e   t .   S e e   l u a _ t y p e   f o r   t h e   e n c o d i n g   o f   t y p e s   f o r   t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ c h e c k u d a t a 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 v o i d   * l u a L _ c h e c k u d a t a   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   c o n s t   c h a r   * t n a m e ) ; 
 
 C h e c k s   w h e t h e r   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   u s e r d a t a   o f   t h e   t y p e   t n a m e   ( s e e   l u a L _ n e w m e t a t a b l e ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ d o f i l e 
 
 [ - 0 ,   + ? ,   m ]   
 
 
 
 i n t   l u a L _ d o f i l e   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * f i l e n a m e ) ; 
 
 L o a d s   a n d   r u n s   t h e   g i v e n   f i l e .   I t   i s   d e f i n e d   a s   t h e   f o l l o w i n g   m a c r o :   
 
 
 
           ( l u a L _ l o a d f i l e ( L ,   f i l e n a m e )   | |   l u a _ p c a l l ( L ,   0 ,   L U A _ M U L T R E T ,   0 ) ) 
 
 
 
 I t   r e t u r n s   0   i f   t h e r e   a r e   n o   e r r o r s   o r   1   i n   c a s e   o f   e r r o r s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ d o s t r i n g 
 
 [ - 0 ,   + ? ,   m ]   
 
 
 
 i n t   l u a L _ d o s t r i n g   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * s t r ) ; 
 
 L o a d s   a n d   r u n s   t h e   g i v e n   s t r i n g .   I t   i s   d e f i n e d   a s   t h e   f o l l o w i n g   m a c r o :   
 
 
 
           ( l u a L _ l o a d s t r i n g ( L ,   s t r )   | |   l u a _ p c a l l ( L ,   0 ,   L U A _ M U L T R E T ,   0 ) ) 
 
 
 
 I t   r e t u r n s   0   i f   t h e r e   a r e   n o   e r r o r s   o r   1   i n   c a s e   o f   e r r o r s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ e r r o r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ e r r o r   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * f m t ,   . . . ) ; 
 
 R a i s e s   a n   e r r o r .   T h e   e r r o r   m e s s a g e   f o r m a t   i s   g i v e n   b y   f m t   p l u s   a n y   e x t r a   a r g u m e n t s ,   f o l l o w i n g   t h e   s a m e   r u l e s   o f   
 
 l u a _ p u s h f s t r i n g .   I t   a l s o   a d d s   a t   t h e   b e g i n n i n g   o f   t h e   m e s s a g e   t h e   f i l e   n a m e   a n d   t h e   l i n e   n u m b e r   w h e r e   t h e   e r r o r   o c c u r r e d ,   
 
 i f   t h i s   i n f o r m a t i o n   i s   a v a i l a b l e .   
 
 
 
 T h i s   f u n c t i o n   n e v e r   r e t u r n s ,   b u t   i t   i s   a n   i d i o m   t o   u s e   i t   i n   C   f u n c t i o n s   a s   r e t u r n   l u a L _ e r r o r ( a r g s ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ g e t m e t a f i e l d 
 
 [ - 0 ,   + ( 0 | 1 ) ,   m ]   
 
 
 
 i n t   l u a L _ g e t m e t a f i e l d   ( l u a _ S t a t e   * L ,   i n t   o b j ,   c o n s t   c h a r   * e ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   f i e l d   e   f r o m   t h e   m e t a t a b l e   o f   t h e   o b j e c t   a t   i n d e x   o b j .   I f   t h e   o b j e c t   d o e s   n o t   h a v e   a   m e t a t a b l e ,   
 
 o r   i f   t h e   m e t a t a b l e   d o e s   n o t   h a v e   t h i s   f i e l d ,   r e t u r n s   0   a n d   p u s h e s   n o t h i n g .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ g e t m e t a t a b l e 
 
 [ - 0 ,   + 1 ,   - ]   
 
 
 
 v o i d   l u a L _ g e t m e t a t a b l e   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * t n a m e ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   t h e   m e t a t a b l e   a s s o c i a t e d   w i t h   n a m e   t n a m e   i n   t h e   r e g i s t r y   ( s e e   l u a L _ n e w m e t a t a b l e ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ g s u b 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 c o n s t   c h a r   * l u a L _ g s u b   ( l u a _ S t a t e   * L , 
 
                                               c o n s t   c h a r   * s , 
 
                                               c o n s t   c h a r   * p , 
 
                                               c o n s t   c h a r   * r ) ; 
 
 C r e a t e s   a   c o p y   o f   s t r i n g   s   b y   r e p l a c i n g   a n y   o c c u r r e n c e   o f   t h e   s t r i n g   p   w i t h   t h e   s t r i n g   r .   P u s h e s   t h e   r e s u l t i n g   s t r i n g   o n   
 
 t h e   s t a c k   a n d   r e t u r n s   i t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ l o a d b u f f e r 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 i n t   l u a L _ l o a d b u f f e r   ( l u a _ S t a t e   * L , 
 
                                           c o n s t   c h a r   * b u f f , 
 
                                           s i z e _ t   s z , 
 
                                           c o n s t   c h a r   * n a m e ) ; 
 
 L o a d s   a   b u f f e r   a s   a   L u a   c h u n k .   T h i s   f u n c t i o n   u s e s   l u a _ l o a d   t o   l o a d   t h e   c h u n k   i n   t h e   b u f f e r   p o i n t e d   t o   b y   b u f f   w i t h   s i z e   
 
 s z .   
 
 
 
 T h i s   f u n c t i o n   r e t u r n s   t h e   s a m e   r e s u l t s   a s   l u a _ l o a d .   n a m e   i s   t h e   c h u n k   n a m e ,   u s e d   f o r   d e b u g   i n f o r m a t i o n   a n d   e r r o r   
 
 m e s s a g e s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ l o a d f i l e 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 i n t   l u a L _ l o a d f i l e   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * f i l e n a m e ) ; 
 
 L o a d s   a   f i l e   a s   a   L u a   c h u n k .   T h i s   f u n c t i o n   u s e s   l u a _ l o a d   t o   l o a d   t h e   c h u n k   i n   t h e   f i l e   n a m e d   f i l e n a m e .   I f   f i l e n a m e   i s   N U L L ,   
 
 t h e n   i t   l o a d s   f r o m   t h e   s t a n d a r d   i n p u t .   T h e   f i r s t   l i n e   i n   t h e   f i l e   i s   i g n o r e d   i f   i t   s t a r t s   w i t h   a   # .   
 
 
 
 T h i s   f u n c t i o n   r e t u r n s   t h e   s a m e   r e s u l t s   a s   l u a _ l o a d ,   b u t   i t   h a s   a n   e x t r a   e r r o r   c o d e   L U A _ E R R F I L E   i f   i t   c a n n o t   o p e n / r e a d   t h e   
 
 f i l e .   
 
 
 
 A s   l u a _ l o a d ,   t h i s   f u n c t i o n   o n l y   l o a d s   t h e   c h u n k ;   i t   d o e s   n o t   r u n   i t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ l o a d s t r i n g 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 i n t   l u a L _ l o a d s t r i n g   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * s ) ; 
 
 L o a d s   a   s t r i n g   a s   a   L u a   c h u n k .   T h i s   f u n c t i o n   u s e s   l u a _ l o a d   t o   l o a d   t h e   c h u n k   i n   t h e   z e r o - t e r m i n a t e d   s t r i n g   s .   
 
 
 
 T h i s   f u n c t i o n   r e t u r n s   t h e   s a m e   r e s u l t s   a s   l u a _ l o a d .   
 
 
 
 A l s o   a s   l u a _ l o a d ,   t h i s   f u n c t i o n   o n l y   l o a d s   t h e   c h u n k ;   i t   d o e s   n o t   r u n   i t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ n e w m e t a t a b l e 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 i n t   l u a L _ n e w m e t a t a b l e   ( l u a _ S t a t e   * L ,   c o n s t   c h a r   * t n a m e ) ; 
 
 I f   t h e   r e g i s t r y   a l r e a d y   h a s   t h e   k e y   t n a m e ,   r e t u r n s   0 .   O t h e r w i s e ,   c r e a t e s   a   n e w   t a b l e   t o   b e   u s e d   a s   a   m e t a t a b l e   f o r   
 
 u s e r d a t a ,   a d d s   i t   t o   t h e   r e g i s t r y   w i t h   k e y   t n a m e ,   a n d   r e t u r n s   1 .   
 
 
 
 I n   b o t h   c a s e s   p u s h e s   o n t o   t h e   s t a c k   t h e   f i n a l   v a l u e   a s s o c i a t e d   w i t h   t n a m e   i n   t h e   r e g i s t r y .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ n e w s t a t e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 l u a _ S t a t e   * l u a L _ n e w s t a t e   ( v o i d ) ; 
 
 C r e a t e s   a   n e w   L u a   s t a t e .   I t   c a l l s   l u a _ n e w s t a t e   w i t h   a n   a l l o c a t o r   b a s e d   o n   t h e   s t a n d a r d   C   r e a l l o c   f u n c t i o n   a n d   t h e n   s e t s   a   
 
 p a n i c   f u n c t i o n   ( s e e   l u a _ a t p a n i c )   t h a t   p r i n t s   a n   e r r o r   m e s s a g e   t o   t h e   s t a n d a r d   e r r o r   o u t p u t   i n   c a s e   o f   f a t a l   e r r o r s .   
 
 
 
 R e t u r n s   t h e   n e w   s t a t e ,   o r   N U L L   i f   t h e r e   i s   a   m e m o r y   a l l o c a t i o n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p e n l i b s 
 
 [ - 0 ,   + 0 ,   m ]   
 
 
 
 v o i d   l u a L _ o p e n l i b s   ( l u a _ S t a t e   * L ) ; 
 
 O p e n s   a l l   s t a n d a r d   L u a   l i b r a r i e s   i n t o   t h e   g i v e n   s t a t e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t i n t 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ o p t i n t   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   i n t   d ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r ,   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a n   i n t .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   
 
 r e t u r n s   d .   O t h e r w i s e ,   r a i s e s   a n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t i n t e g e r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l u a _ I n t e g e r   l u a L _ o p t i n t e g e r   ( l u a _ S t a t e   * L , 
 
                                                           i n t   n a r g , 
 
                                                           l u a _ I n t e g e r   d ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r ,   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a   l u a _ I n t e g e r .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   
 
 r e t u r n s   d .   O t h e r w i s e ,   r a i s e s   a n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t l o n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l o n g   l u a L _ o p t l o n g   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   l o n g   d ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r ,   r e t u r n s   t h i s   n u m b e r   c a s t   t o   a   l o n g .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   
 
 r e t u r n s   d .   O t h e r w i s e ,   r a i s e s   a n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t l s t r i n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 c o n s t   c h a r   * l u a L _ o p t l s t r i n g   ( l u a _ S t a t e   * L , 
 
                                                           i n t   n a r g , 
 
                                                           c o n s t   c h a r   * d , 
 
                                                           s i z e _ t   * l ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   s t r i n g ,   r e t u r n s   t h i s   s t r i n g .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   r e t u r n s   d .   O t h e r w i s e ,   
 
 r a i s e s   a n   e r r o r .   
 
 
 
 I f   l   i s   n o t   N U L L ,   f i l l s   t h e   p o s i t i o n   * l   w i t h   t h e   r e s u l t s ' s   l e n g t h .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t n u m b e r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 l u a _ N u m b e r   l u a L _ o p t n u m b e r   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   l u a _ N u m b e r   d ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   n u m b e r ,   r e t u r n s   t h i s   n u m b e r .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   r e t u r n s   d .   O t h e r w i s e ,   
 
 r a i s e s   a n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ o p t s t r i n g 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 c o n s t   c h a r   * l u a L _ o p t s t r i n g   ( l u a _ S t a t e   * L , 
 
                                                         i n t   n a r g , 
 
                                                         c o n s t   c h a r   * d ) ; 
 
 I f   t h e   f u n c t i o n   a r g u m e n t   n a r g   i s   a   s t r i n g ,   r e t u r n s   t h i s   s t r i n g .   I f   t h i s   a r g u m e n t   i s   a b s e n t   o r   i s   n i l ,   r e t u r n s   d .   O t h e r w i s e ,   
 
 r a i s e s   a n   e r r o r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ p r e p b u f f e r 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 c h a r   * l u a L _ p r e p b u f f e r   ( l u a L _ B u f f e r   * B ) ; 
 
 R e t u r n s   a n   a d d r e s s   t o   a   s p a c e   o f   s i z e   L U A L _ B U F F E R S I Z E   w h e r e   y o u   c a n   c o p y   a   s t r i n g   t o   b e   a d d e d   t o   b u f f e r   B   ( s e e   l u a L _ B u f f e r ) .   
 
 A f t e r   c o p y i n g   t h e   s t r i n g   i n t o   t h i s   s p a c e   y o u   m u s t   c a l l   l u a L _ a d d s i z e   w i t h   t h e   s i z e   o f   t h e   s t r i n g   t o   a c t u a l l y   a d d   i t   t o   t h e   
 
 b u f f e r .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ p u s h r e s u l t 
 
 [ - ? ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a L _ p u s h r e s u l t   ( l u a L _ B u f f e r   * B ) ; 
 
 F i n i s h e s   t h e   u s e   o f   b u f f e r   B   l e a v i n g   t h e   f i n a l   s t r i n g   o n   t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ r e f 
 
 [ - 1 ,   + 0 ,   m ]   
 
 
 
 i n t   l u a L _ r e f   ( l u a _ S t a t e   * L ,   i n t   t ) ; 
 
 C r e a t e s   a n d   r e t u r n s   a   r e f e r e n c e ,   i n   t h e   t a b l e   a t   i n d e x   t ,   f o r   t h e   o b j e c t   a t   t h e   t o p   o f   t h e   s t a c k   ( a n d   p o p s   t h e   o b j e c t ) .   
 
 
 
 A   r e f e r e n c e   i s   a   u n i q u e   i n t e g e r   k e y .   A s   l o n g   a s   y o u   d o   n o t   m a n u a l l y   a d d   i n t e g e r   k e y s   i n t o   t a b l e   t ,   l u a L _ r e f   e n s u r e s   t h e   
 
 u n i q u e n e s s   o f   t h e   k e y   i t   r e t u r n s .   Y o u   c a n   r e t r i e v e   a n   o b j e c t   r e f e r r e d   b y   r e f e r e n c e   r   b y   c a l l i n g   l u a _ r a w g e t i ( L ,   t ,   r ) .   
 
 F u n c t i o n   l u a L _ u n r e f   f r e e s   a   r e f e r e n c e   a n d   i t s   a s s o c i a t e d   o b j e c t .   
 
 
 
 I f   t h e   o b j e c t   a t   t h e   t o p   o f   t h e   s t a c k   i s   n i l ,   l u a L _ r e f   r e t u r n s   t h e   c o n s t a n t   L U A _ R E F N I L .   T h e   c o n s t a n t   L U A _ N O R E F   i s   
 
 g u a r a n t e e d   t o   b e   d i f f e r e n t   f r o m   a n y   r e f e r e n c e   r e t u r n e d   b y   l u a L _ r e f .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ R e g 
 
 t y p e d e f   s t r u c t   l u a L _ R e g   { 
 
     c o n s t   c h a r   * n a m e ; 
 
     l u a _ C F u n c t i o n   f u n c ; 
 
 }   l u a L _ R e g ; 
 
 T y p e   f o r   a r r a y s   o f   f u n c t i o n s   t o   b e   r e g i s t e r e d   b y   l u a L _ r e g i s t e r .   n a m e   i s   t h e   f u n c t i o n   n a m e   a n d   f u n c   i s   a   p o i n t e r   t o   t h e   
 
 f u n c t i o n .   A n y   a r r a y   o f   l u a L _ R e g   m u s t   e n d   w i t h   a n   s e n t i n e l   e n t r y   i n   w h i c h   b o t h   n a m e   a n d   f u n c   a r e   N U L L .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ r e g i s t e r 
 
 [ - ( 0 | 1 ) ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a L _ r e g i s t e r   ( l u a _ S t a t e   * L , 
 
                                         c o n s t   c h a r   * l i b n a m e , 
 
                                         c o n s t   l u a L _ R e g   * l ) ; 
 
 O p e n s   a   l i b r a r y .   
 
 
 
 W h e n   c a l l e d   w i t h   l i b n a m e   e q u a l   t o   N U L L ,   i t   s i m p l y   r e g i s t e r s   a l l   f u n c t i o n s   i n   t h e   l i s t   l   ( s e e   l u a L _ R e g )   i n t o   t h e   t a b l e   o n   
 
 t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 W h e n   c a l l e d   w i t h   a   n o n - n u l l   l i b n a m e ,   l u a L _ r e g i s t e r   c r e a t e s   a   n e w   t a b l e   t ,   s e t s   i t   a s   t h e   v a l u e   o f   t h e   g l o b a l   v a r i a b l e   
 
 l i b n a m e ,   s e t s   i t   a s   t h e   v a l u e   o f   p a c k a g e . l o a d e d [ l i b n a m e ] ,   a n d   r e g i s t e r s   o n   i t   a l l   f u n c t i o n s   i n   t h e   l i s t   l .   I f   t h e r e   i s   a   
 
 t a b l e   i n   p a c k a g e . l o a d e d [ l i b n a m e ]   o r   i n   v a r i a b l e   l i b n a m e ,   r e u s e s   t h i s   t a b l e   i n s t e a d   o f   c r e a t i n g   a   n e w   o n e .   
 
 
 
 I n   a n y   c a s e   t h e   f u n c t i o n   l e a v e s   t h e   t a b l e   o n   t h e   t o p   o f   t h e   s t a c k .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ t y p e n a m e 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 c o n s t   c h a r   * l u a L _ t y p e n a m e   ( l u a _ S t a t e   * L ,   i n t   i n d e x ) ; 
 
 R e t u r n s   t h e   n a m e   o f   t h e   t y p e   o f   t h e   v a l u e   a t   t h e   g i v e n   i n d e x .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ t y p e r r o r 
 
 [ - 0 ,   + 0 ,   v ]   
 
 
 
 i n t   l u a L _ t y p e r r o r   ( l u a _ S t a t e   * L ,   i n t   n a r g ,   c o n s t   c h a r   * t n a m e ) ; 
 
 G e n e r a t e s   a n   e r r o r   w i t h   a   m e s s a g e   l i k e   t h e   f o l l o w i n g :   
 
 
 
           l o c a t i o n :   b a d   a r g u m e n t   n a r g   t o   ' f u n c '   ( t n a m e   e x p e c t e d ,   g o t   r t ) 
 
 
 
 w h e r e   l o c a t i o n   i s   p r o d u c e d   b y   l u a L _ w h e r e ,   f u n c   i s   t h e   n a m e   o f   t h e   c u r r e n t   f u n c t i o n ,   a n d   r t   i s   t h e   t y p e   n a m e   o f   t h e   a c t u a l   
 
 a r g u m e n t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ u n r e f 
 
 [ - 0 ,   + 0 ,   - ]   
 
 
 
 v o i d   l u a L _ u n r e f   ( l u a _ S t a t e   * L ,   i n t   t ,   i n t   r e f ) ; 
 
 R e l e a s e s   r e f e r e n c e   r e f   f r o m   t h e   t a b l e   a t   i n d e x   t   ( s e e   l u a L _ r e f ) .   T h e   e n t r y   i s   r e m o v e d   f r o m   t h e   t a b l e ,   s o   t h a t   t h e   r e f e r r e d   
 
 o b j e c t   c a n   b e   c o l l e c t e d .   T h e   r e f e r e n c e   r e f   i s   a l s o   f r e e d   t o   b e   u s e d   a g a i n .   
 
 
 
 I f   r e f   i s   L U A _ N O R E F   o r   L U A _ R E F N I L ,   l u a L _ u n r e f   d o e s   n o t h i n g .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l u a L _ w h e r e 
 
 [ - 0 ,   + 1 ,   m ]   
 
 
 
 v o i d   l u a L _ w h e r e   ( l u a _ S t a t e   * L ,   i n t   l v l ) ; 
 
 P u s h e s   o n t o   t h e   s t a c k   a   s t r i n g   i d e n t i f y i n g   t h e   c u r r e n t   p o s i t i o n   o f   t h e   c o n t r o l   a t   l e v e l   l v l   i n   t h e   c a l l   s t a c k .   T y p i c a l l y   
 
 t h i s   s t r i n g   h a s   t h e   f o l l o w i n g   f o r m a t :   
 
 
 
           c h u n k n a m e : c u r r e n t l i n e : 
 
 
 
 L e v e l   0   i s   t h e   r u n n i n g   f u n c t i o n ,   l e v e l   1   i s   t h e   f u n c t i o n   t h a t   c a l l e d   t h e   r u n n i n g   f u n c t i o n ,   e t c .   
 
 
 
 T h i s   f u n c t i o n   i s   u s e d   t o   b u i l d   a   p r e f i x   f o r   e r r o r   m e s s a g e s .   
 
 
 
 5   -   S t a n d a r d   L i b r a r i e s 
 
 T h e   s t a n d a r d   L u a   l i b r a r i e s   p r o v i d e   u s e f u l   f u n c t i o n s   t h a t   a r e   i m p l e m e n t e d   d i r e c t l y   t h r o u g h   t h e   C   A P I .   S o m e   o f   t h e s e   f u n c t i o n s   
 
 p r o v i d e   e s s e n t i a l   s e r v i c e s   t o   t h e   l a n g u a g e   ( e . g . ,   t y p e   a n d   g e t m e t a t a b l e ) ;   o t h e r s   p r o v i d e   a c c e s s   t o   " o u t s i d e "   s e r v i c e s   ( e . g . ,   
 
 I / O ) ;   a n d   o t h e r s   c o u l d   b e   i m p l e m e n t e d   i n   L u a   i t s e l f ,   b u t   a r e   q u i t e   u s e f u l   o r   h a v e   c r i t i c a l   p e r f o r m a n c e   r e q u i r e m e n t s   t h a t   
 
 d e s e r v e   a n   i m p l e m e n t a t i o n   i n   C   ( e . g . ,   t a b l e . s o r t ) .   
 
 
 
 A l l   l i b r a r i e s   a r e   i m p l e m e n t e d   t h r o u g h   t h e   o f f i c i a l   C   A P I   a n d   a r e   p r o v i d e d   a s   s e p a r a t e   C   m o d u l e s .   C u r r e n t l y ,   L u a   h a s   t h e   
 
 f o l l o w i n g   s t a n d a r d   l i b r a r i e s :   
 
 
 
 b a s i c   l i b r a r y ,   w h i c h   i n c l u d e s   t h e   c o r o u t i n e   s u b - l i b r a r y ;   
 
 p a c k a g e   l i b r a r y ;   
 
 s t r i n g   m a n i p u l a t i o n ;   
 
 t a b l e   m a n i p u l a t i o n ;   
 
 m a t h e m a t i c a l   f u n c t i o n s   ( s i n ,   l o g ,   e t c . ) ;   
 
 i n p u t   a n d   o u t p u t ;   
 
 o p e r a t i n g   s y s t e m   f a c i l i t i e s ;   
 
 d e b u g   f a c i l i t i e s .   
 
 E x c e p t   f o r   t h e   b a s i c   a n d   p a c k a g e   l i b r a r i e s ,   e a c h   l i b r a r y   p r o v i d e s   a l l   i t s   f u n c t i o n s   a s   f i e l d s   o f   a   g l o b a l   t a b l e   o r   a s   
 
 m e t h o d s   o f   i t s   o b j e c t s .   
 
 
 
 T o   h a v e   a c c e s s   t o   t h e s e   l i b r a r i e s ,   t h e   C   h o s t   p r o g r a m   s h o u l d   c a l l   t h e   l u a L _ o p e n l i b s   f u n c t i o n ,   w h i c h   o p e n s   a l l   s t a n d a r d   
 
 l i b r a r i e s .   A l t e r n a t i v e l y ,   i t   c a n   o p e n   t h e m   i n d i v i d u a l l y   b y   c a l l i n g   l u a o p e n _ b a s e   ( f o r   t h e   b a s i c   l i b r a r y ) ,   l u a o p e n _ p a c k a g e   
 
 ( f o r   t h e   p a c k a g e   l i b r a r y ) ,   l u a o p e n _ s t r i n g   ( f o r   t h e   s t r i n g   l i b r a r y ) ,   l u a o p e n _ t a b l e   ( f o r   t h e   t a b l e   l i b r a r y ) ,   l u a o p e n _ m a t h   
 
 ( f o r   t h e   m a t h e m a t i c a l   l i b r a r y ) ,   l u a o p e n _ i o   ( f o r   t h e   I / O   l i b r a r y ) ,   l u a o p e n _ o s   ( f o r   t h e   O p e r a t i n g   S y s t e m   l i b r a r y ) ,   a n d   
 
 l u a o p e n _ d e b u g   ( f o r   t h e   d e b u g   l i b r a r y ) .   T h e s e   f u n c t i o n s   a r e   d e c l a r e d   i n   l u a l i b . h   a n d   s h o u l d   n o t   b e   c a l l e d   d i r e c t l y :   y o u   m u s t   
 
 c a l l   t h e m   l i k e   a n y   o t h e r   L u a   C   f u n c t i o n ,   e . g . ,   b y   u s i n g   l u a _ c a l l .   
 
 
 
 5 . 1   -   B a s i c   F u n c t i o n s 
 
 T h e   b a s i c   l i b r a r y   p r o v i d e s   s o m e   c o r e   f u n c t i o n s   t o   L u a .   I f   y o u   d o   n o t   i n c l u d e   t h i s   l i b r a r y   i n   y o u r   a p p l i c a t i o n ,   y o u   s h o u l d   
 
 c h e c k   c a r e f u l l y   w h e t h e r   y o u   n e e d   t o   p r o v i d e   i m p l e m e n t a t i o n s   f o r   s o m e   o f   i t s   f a c i l i t i e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 a s s e r t   ( v   [ ,   m e s s a g e ] ) 
 
 I s s u e s   a n   e r r o r   w h e n   t h e   v a l u e   o f   i t s   a r g u m e n t   v   i s   f a l s e   ( i . e . ,   n i l   o r   f a l s e ) ;   o t h e r w i s e ,   r e t u r n s   a l l   i t s   a r g u m e n t s .   
 
 m e s s a g e   i s   a n   e r r o r   m e s s a g e ;   w h e n   a b s e n t ,   i t   d e f a u l t s   t o   " a s s e r t i o n   f a i l e d ! "   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o l l e c t g a r b a g e   ( [ o p t   [ ,   a r g ] ] ) 
 
 T h i s   f u n c t i o n   i s   a   g e n e r i c   i n t e r f a c e   t o   t h e   g a r b a g e   c o l l e c t o r .   I t   p e r f o r m s   d i f f e r e n t   f u n c t i o n s   a c c o r d i n g   t o   i t s   f i r s t   
 
 a r g u m e n t ,   o p t :   
 
 
 
 " c o l l e c t " :   p e r f o r m s   a   f u l l   g a r b a g e - c o l l e c t i o n   c y c l e .   T h i s   i s   t h e   d e f a u l t   o p t i o n .   
 
 " s t o p " :   s t o p s   t h e   g a r b a g e   c o l l e c t o r .   
 
 " r e s t a r t " :   r e s t a r t s   t h e   g a r b a g e   c o l l e c t o r .   
 
 " c o u n t " :   r e t u r n s   t h e   t o t a l   m e m o r y   i n   u s e   b y   L u a   ( i n   K b y t e s ) .   
 
 " s t e p " :   p e r f o r m s   a   g a r b a g e - c o l l e c t i o n   s t e p .   T h e   s t e p   " s i z e "   i s   c o n t r o l l e d   b y   a r g   ( l a r g e r   v a l u e s   m e a n   m o r e   s t e p s )   i n   a   
 
 n o n - s p e c i f i e d   w a y .   I f   y o u   w a n t   t o   c o n t r o l   t h e   s t e p   s i z e   y o u   m u s t   e x p e r i m e n t a l l y   t u n e   t h e   v a l u e   o f   a r g .   R e t u r n s   t r u e   i f   t h e   
 
 s t e p   f i n i s h e d   a   c o l l e c t i o n   c y c l e .   
 
 " s e t p a u s e " :   s e t s   a r g   a s   t h e   n e w   v a l u e   f o r   t h e   p a u s e   o f   t h e   c o l l e c t o r   ( s e e    2 . 1 0 ) .   R e t u r n s   t h e   p r e v i o u s   v a l u e   f o r   p a u s e .   
 
 " s e t s t e p m u l " :   s e t s   a r g   a s   t h e   n e w   v a l u e   f o r   t h e   s t e p   m u l t i p l i e r   o f   t h e   c o l l e c t o r   ( s e e    2 . 1 0 ) .   R e t u r n s   t h e   p r e v i o u s   v a l u e   
 
 f o r   s t e p .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d o f i l e   ( [ f i l e n a m e ] ) 
 
 O p e n s   t h e   n a m e d   f i l e   a n d   e x e c u t e s   i t s   c o n t e n t s   a s   a   L u a   c h u n k .   W h e n   c a l l e d   w i t h o u t   a r g u m e n t s ,   d o f i l e   e x e c u t e s   t h e   c o n t e n t s   
 
 o f   t h e   s t a n d a r d   i n p u t   ( s t d i n ) .   R e t u r n s   a l l   v a l u e s   r e t u r n e d   b y   t h e   c h u n k .   I n   c a s e   o f   e r r o r s ,   d o f i l e   p r o p a g a t e s   t h e   e r r o r   t o   
 
 i t s   c a l l e r   ( t h a t   i s ,   d o f i l e   d o e s   n o t   r u n   i n   p r o t e c t e d   m o d e ) .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 e r r o r   ( m e s s a g e   [ ,   l e v e l ] ) 
 
 T e r m i n a t e s   t h e   l a s t   p r o t e c t e d   f u n c t i o n   c a l l e d   a n d   r e t u r n s   m e s s a g e   a s   t h e   e r r o r   m e s s a g e .   F u n c t i o n   e r r o r   n e v e r   r e t u r n s .   
 
 U s u a l l y ,   e r r o r   a d d s   s o m e   i n f o r m a t i o n   a b o u t   t h e   e r r o r   p o s i t i o n   a t   t h e   b e g i n n i n g   o f   t h e   m e s s a g e .   T h e   l e v e l   a r g u m e n t   s p e c i f i e s   
 
 h o w   t o   g e t   t h e   e r r o r   p o s i t i o n .   W i t h   l e v e l   1   ( t h e   d e f a u l t ) ,   t h e   e r r o r   p o s i t i o n   i s   w h e r e   t h e   e r r o r   f u n c t i o n   w a s   c a l l e d .   L e v e l   
 
 2   p o i n t s   t h e   e r r o r   t o   w h e r e   t h e   f u n c t i o n   t h a t   c a l l e d   e r r o r   w a s   c a l l e d ;   a n d   s o   o n .   P a s s i n g   a   l e v e l   0   a v o i d s   t h e   a d d i t i o n   o f   
 
 e r r o r   p o s i t i o n   i n f o r m a t i o n   t o   t h e   m e s s a g e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 _ G 
 
 A   g l o b a l   v a r i a b l e   ( n o t   a   f u n c t i o n )   t h a t   h o l d s   t h e   g l o b a l   e n v i r o n m e n t   ( t h a t   i s ,   _ G . _ G   =   _ G ) .   L u a   i t s e l f   d o e s   n o t   u s e   t h i s   
 
 v a r i a b l e ;   c h a n g i n g   i t s   v a l u e   d o e s   n o t   a f f e c t   a n y   e n v i r o n m e n t ,   n o r   v i c e - v e r s a .   ( U s e   s e t f e n v   t o   c h a n g e   e n v i r o n m e n t s . )   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 g e t f e n v   ( [ f ] ) 
 
 R e t u r n s   t h e   c u r r e n t   e n v i r o n m e n t   i n   u s e   b y   t h e   f u n c t i o n .   f   c a n   b e   a   L u a   f u n c t i o n   o r   a   n u m b e r   t h a t   s p e c i f i e s   t h e   f u n c t i o n   a t   
 
 t h a t   s t a c k   l e v e l :   L e v e l   1   i s   t h e   f u n c t i o n   c a l l i n g   g e t f e n v .   I f   t h e   g i v e n   f u n c t i o n   i s   n o t   a   L u a   f u n c t i o n ,   o r   i f   f   i s   0 ,   
 
 g e t f e n v   r e t u r n s   t h e   g l o b a l   e n v i r o n m e n t .   T h e   d e f a u l t   f o r   f   i s   1 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 g e t m e t a t a b l e   ( o b j e c t ) 
 
 I f   o b j e c t   d o e s   n o t   h a v e   a   m e t a t a b l e ,   r e t u r n s   n i l .   O t h e r w i s e ,   i f   t h e   o b j e c t ' s   m e t a t a b l e   h a s   a   " _ _ m e t a t a b l e "   f i e l d ,   r e t u r n s   
 
 t h e   a s s o c i a t e d   v a l u e .   O t h e r w i s e ,   r e t u r n s   t h e   m e t a t a b l e   o f   t h e   g i v e n   o b j e c t .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i p a i r s   ( t ) 
 
 R e t u r n s   t h r e e   v a l u e s :   a n   i t e r a t o r   f u n c t i o n ,   t h e   t a b l e   t ,   a n d   0 ,   s o   t h a t   t h e   c o n s t r u c t i o n   
 
 
 
           f o r   i , v   i n   i p a i r s ( t )   d o   b o d y   e n d 
 
 
 
 w i l l   i t e r a t e   o v e r   t h e   p a i r s   ( 1 , t [ 1 ] ) ,   ( 2 , t [ 2 ] ) ,      ,   u p   t o   t h e   f i r s t   i n t e g e r   k e y   a b s e n t   f r o m   t h e   t a b l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l o a d   ( f u n c   [ ,   c h u n k n a m e ] ) 
 
 L o a d s   a   c h u n k   u s i n g   f u n c t i o n   f u n c   t o   g e t   i t s   p i e c e s .   E a c h   c a l l   t o   f u n c   m u s t   r e t u r n   a   s t r i n g   t h a t   c o n c a t e n a t e s   w i t h   p r e v i o u s   
 
 r e s u l t s .   A   r e t u r n   o f   a n   e m p t y   s t r i n g ,   n i l ,   o r   n o   v a l u e   s i g n a l s   t h e   e n d   o f   t h e   c h u n k .   
 
 
 
 I f   t h e r e   a r e   n o   e r r o r s ,   r e t u r n s   t h e   c o m p i l e d   c h u n k   a s   a   f u n c t i o n ;   o t h e r w i s e ,   r e t u r n s   n i l   p l u s   t h e   e r r o r   m e s s a g e .   T h e   
 
 e n v i r o n m e n t   o f   t h e   r e t u r n e d   f u n c t i o n   i s   t h e   g l o b a l   e n v i r o n m e n t .   
 
 
 
 c h u n k n a m e   i s   u s e d   a s   t h e   c h u n k   n a m e   f o r   e r r o r   m e s s a g e s   a n d   d e b u g   i n f o r m a t i o n .   W h e n   a b s e n t ,   i t   d e f a u l t s   t o   " = ( l o a d ) " .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l o a d f i l e   ( [ f i l e n a m e ] ) 
 
 S i m i l a r   t o   l o a d ,   b u t   g e t s   t h e   c h u n k   f r o m   f i l e   f i l e n a m e   o r   f r o m   t h e   s t a n d a r d   i n p u t ,   i f   n o   f i l e   n a m e   i s   g i v e n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 l o a d s t r i n g   ( s t r i n g   [ ,   c h u n k n a m e ] ) 
 
 S i m i l a r   t o   l o a d ,   b u t   g e t s   t h e   c h u n k   f r o m   t h e   g i v e n   s t r i n g .   
 
 
 
 T o   l o a d   a n d   r u n   a   g i v e n   s t r i n g ,   u s e   t h e   i d i o m   
 
 
 
           a s s e r t ( l o a d s t r i n g ( s ) ) ( ) 
 
 
 
 W h e n   a b s e n t ,   c h u n k n a m e   d e f a u l t s   t o   t h e   g i v e n   s t r i n g .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 n e x t   ( t a b l e   [ ,   i n d e x ] ) 
 
 A l l o w s   a   p r o g r a m   t o   t r a v e r s e   a l l   f i e l d s   o f   a   t a b l e .   I t s   f i r s t   a r g u m e n t   i s   a   t a b l e   a n d   i t s   s e c o n d   a r g u m e n t   i s   a n   i n d e x   i n   
 
 t h i s   t a b l e .   n e x t   r e t u r n s   t h e   n e x t   i n d e x   o f   t h e   t a b l e   a n d   i t s   a s s o c i a t e d   v a l u e .   W h e n   c a l l e d   w i t h   n i l   a s   i t s   s e c o n d   a r g u m e n t ,   
 
 n e x t   r e t u r n s   a n   i n i t i a l   i n d e x   a n d   i t s   a s s o c i a t e d   v a l u e .   W h e n   c a l l e d   w i t h   t h e   l a s t   i n d e x ,   o r   w i t h   n i l   i n   a n   e m p t y   t a b l e ,   n e x t   
 
 r e t u r n s   n i l .   I f   t h e   s e c o n d   a r g u m e n t   i s   a b s e n t ,   t h e n   i t   i s   i n t e r p r e t e d   a s   n i l .   I n   p a r t i c u l a r ,   y o u   c a n   u s e   n e x t ( t )   t o   c h e c k   
 
 w h e t h e r   a   t a b l e   i s   e m p t y .   
 
 
 
 T h e   o r d e r   i n   w h i c h   t h e   i n d i c e s   a r e   e n u m e r a t e d   i s   n o t   s p e c i f i e d ,   e v e n   f o r   n u m e r i c   i n d i c e s .   ( T o   t r a v e r s e   a   t a b l e   i n   n u m e r i c   
 
 o r d e r ,   u s e   a   n u m e r i c a l   f o r   o r   t h e   i p a i r s   f u n c t i o n . )   
 
 
 
 T h e   b e h a v i o r   o f   n e x t   i s   u n d e f i n e d   i f ,   d u r i n g   t h e   t r a v e r s a l ,   y o u   a s s i g n   a n y   v a l u e   t o   a   n o n - e x i s t e n t   f i e l d   i n   t h e   t a b l e .   Y o u   
 
 m a y   h o w e v e r   m o d i f y   e x i s t i n g   f i e l d s .   I n   p a r t i c u l a r ,   y o u   m a y   c l e a r   e x i s t i n g   f i e l d s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a i r s   ( t ) 
 
 R e t u r n s   t h r e e   v a l u e s :   t h e   n e x t   f u n c t i o n ,   t h e   t a b l e   t ,   a n d   n i l ,   s o   t h a t   t h e   c o n s t r u c t i o n   
 
 
 
           f o r   k , v   i n   p a i r s ( t )   d o   b o d y   e n d 
 
 
 
 w i l l   i t e r a t e   o v e r   a l l   k e y  v a l u e   p a i r s   o f   t a b l e   t .   
 
 
 
 S e e   f u n c t i o n   n e x t   f o r   t h e   c a v e a t s   o f   m o d i f y i n g   t h e   t a b l e   d u r i n g   i t s   t r a v e r s a l .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p c a l l   ( f ,   a r g 1 ,      ) 
 
 C a l l s   f u n c t i o n   f   w i t h   t h e   g i v e n   a r g u m e n t s   i n   p r o t e c t e d   m o d e .   T h i s   m e a n s   t h a t   a n y   e r r o r   i n s i d e   f   i s   n o t   p r o p a g a t e d ;   i n s t e a d ,   
 
 p c a l l   c a t c h e s   t h e   e r r o r   a n d   r e t u r n s   a   s t a t u s   c o d e .   I t s   f i r s t   r e s u l t   i s   t h e   s t a t u s   c o d e   ( a   b o o l e a n ) ,   w h i c h   i s   t r u e   i f   t h e   
 
 c a l l   s u c c e e d s   w i t h o u t   e r r o r s .   I n   s u c h   c a s e ,   p c a l l   a l s o   r e t u r n s   a l l   r e s u l t s   f r o m   t h e   c a l l ,   a f t e r   t h i s   f i r s t   r e s u l t .   I n   c a s e   
 
 o f   a n y   e r r o r ,   p c a l l   r e t u r n s   f a l s e   p l u s   t h e   e r r o r   m e s s a g e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p r i n t   (    ) 
 
 R e c e i v e s   a n y   n u m b e r   o f   a r g u m e n t s ,   a n d   p r i n t s   t h e i r   v a l u e s   t o   s t d o u t ,   u s i n g   t h e   t o s t r i n g   f u n c t i o n   t o   c o n v e r t   t h e m   t o   
 
 s t r i n g s .   p r i n t   i s   n o t   i n t e n d e d   f o r   f o r m a t t e d   o u t p u t ,   b u t   o n l y   a s   a   q u i c k   w a y   t o   s h o w   a   v a l u e ,   t y p i c a l l y   f o r   d e b u g g i n g .   F o r   
 
 f o r m a t t e d   o u t p u t ,   u s e   s t r i n g . f o r m a t .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 r a w e q u a l   ( v 1 ,   v 2 ) 
 
 C h e c k s   w h e t h e r   v 1   i s   e q u a l   t o   v 2 ,   w i t h o u t   i n v o k i n g   a n y   m e t a m e t h o d .   R e t u r n s   a   b o o l e a n .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 r a w g e t   ( t a b l e ,   i n d e x ) 
 
 G e t s   t h e   r e a l   v a l u e   o f   t a b l e [ i n d e x ] ,   w i t h o u t   i n v o k i n g   a n y   m e t a m e t h o d .   t a b l e   m u s t   b e   a   t a b l e ;   i n d e x   m a y   b e   a n y   v a l u e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 r a w s e t   ( t a b l e ,   i n d e x ,   v a l u e ) 
 
 S e t s   t h e   r e a l   v a l u e   o f   t a b l e [ i n d e x ]   t o   v a l u e ,   w i t h o u t   i n v o k i n g   a n y   m e t a m e t h o d .   t a b l e   m u s t   b e   a   t a b l e ,   i n d e x   a n y   v a l u e   
 
 d i f f e r e n t   f r o m   n i l ,   a n d   v a l u e   a n y   L u a   v a l u e .   
 
 T h i s   f u n c t i o n   r e t u r n s   t a b l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s e l e c t   ( i n d e x ,      ) 
 
 I f   i n d e x   i s   a   n u m b e r ,   r e t u r n s   a l l   a r g u m e n t s   a f t e r   a r g u m e n t   n u m b e r   i n d e x .   O t h e r w i s e ,   i n d e x   m u s t   b e   t h e   s t r i n g   " # " ,   a n d   
 
 s e l e c t   r e t u r n s   t h e   t o t a l   n u m b e r   o f   e x t r a   a r g u m e n t s   i t   r e c e i v e d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s e t f e n v   ( f ,   t a b l e ) 
 
 S e t s   t h e   e n v i r o n m e n t   t o   b e   u s e d   b y   t h e   g i v e n   f u n c t i o n .   f   c a n   b e   a   L u a   f u n c t i o n   o r   a   n u m b e r   t h a t   s p e c i f i e s   t h e   f u n c t i o n   a t   
 
 t h a t   s t a c k   l e v e l :   L e v e l   1   i s   t h e   f u n c t i o n   c a l l i n g   s e t f e n v .   s e t f e n v   r e t u r n s   t h e   g i v e n   f u n c t i o n .   
 
 
 
 A s   a   s p e c i a l   c a s e ,   w h e n   f   i s   0   s e t f e n v   c h a n g e s   t h e   e n v i r o n m e n t   o f   t h e   r u n n i n g   t h r e a d .   I n   t h i s   c a s e ,   s e t f e n v   r e t u r n s   n o   
 
 v a l u e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s e t m e t a t a b l e   ( t a b l e ,   m e t a t a b l e ) 
 
 S e t s   t h e   m e t a t a b l e   f o r   t h e   g i v e n   t a b l e .   ( Y o u   c a n n o t   c h a n g e   t h e   m e t a t a b l e   o f   o t h e r   t y p e s   f r o m   L u a ,   o n l y   f r o m   C . )   I f   
 
 m e t a t a b l e   i s   n i l ,   r e m o v e s   t h e   m e t a t a b l e   o f   t h e   g i v e n   t a b l e .   I f   t h e   o r i g i n a l   m e t a t a b l e   h a s   a   " _ _ m e t a t a b l e "   f i e l d ,   r a i s e s   a n   
 
 e r r o r .   
 
 
 
 T h i s   f u n c t i o n   r e t u r n s   t a b l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t o n u m b e r   ( e   [ ,   b a s e ] ) 
 
 T r i e s   t o   c o n v e r t   i t s   a r g u m e n t   t o   a   n u m b e r .   I f   t h e   a r g u m e n t   i s   a l r e a d y   a   n u m b e r   o r   a   s t r i n g   c o n v e r t i b l e   t o   a   n u m b e r ,   t h e n   
 
 t o n u m b e r   r e t u r n s   t h i s   n u m b e r ;   o t h e r w i s e ,   i t   r e t u r n s   n i l .   
 
 A n   o p t i o n a l   a r g u m e n t   s p e c i f i e s   t h e   b a s e   t o   i n t e r p r e t   t h e   n u m e r a l .   T h e   b a s e   m a y   b e   a n y   i n t e g e r   b e t w e e n   2   a n d   3 6 ,   i n c l u s i v e .   I n   
 
 b a s e s   a b o v e   1 0 ,   t h e   l e t t e r   ' A '   ( i n   e i t h e r   u p p e r   o r   l o w e r   c a s e )   r e p r e s e n t s   1 0 ,   ' B '   r e p r e s e n t s   1 1 ,   a n d   s o   f o r t h ,   w i t h   ' Z '   
 
 r e p r e s e n t i n g   3 5 .   I n   b a s e   1 0   ( t h e   d e f a u l t ) ,   t h e   n u m b e r   c a n   h a v e   a   d e c i m a l   p a r t ,   a s   w e l l   a s   a n   o p t i o n a l   e x p o n e n t   p a r t   
 
 ( s e e    2 . 1 ) .   I n   o t h e r   b a s e s ,   o n l y   u n s i g n e d   i n t e g e r s   a r e   a c c e p t e d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t o s t r i n g   ( e ) 
 
 R e c e i v e s   a n   a r g u m e n t   o f   a n y   t y p e   a n d   c o n v e r t s   i t   t o   a   s t r i n g   i n   a   r e a s o n a b l e   f o r m a t .   F o r   c o m p l e t e   c o n t r o l   o f   h o w   n u m b e r s   
 
 a r e   c o n v e r t e d ,   u s e   s t r i n g . f o r m a t .   
 
 I f   t h e   m e t a t a b l e   o f   e   h a s   a   " _ _ t o s t r i n g "   f i e l d ,   t h e n   t o s t r i n g   c a l l s   t h e   c o r r e s p o n d i n g   v a l u e   w i t h   e   a s   a r g u m e n t ,   a n d   u s e s   
 
 t h e   r e s u l t   o f   t h e   c a l l   a s   i t s   r e s u l t .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t y p e   ( v ) 
 
 R e t u r n s   t h e   t y p e   o f   i t s   o n l y   a r g u m e n t ,   c o d e d   a s   a   s t r i n g .   T h e   p o s s i b l e   r e s u l t s   o f   t h i s   f u n c t i o n   a r e   " n i l "   ( a   s t r i n g ,   n o t   
 
 t h e   v a l u e   n i l ) ,   " n u m b e r " ,   " s t r i n g " ,   " b o o l e a n " ,   " t a b l e " ,   " f u n c t i o n " ,   " t h r e a d " ,   a n d   " u s e r d a t a " .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 u n p a c k   ( l i s t   [ ,   i   [ ,   j ] ] ) 
 
 R e t u r n s   t h e   e l e m e n t s   f r o m   t h e   g i v e n   t a b l e .   T h i s   f u n c t i o n   i s   e q u i v a l e n t   t o   
 
           r e t u r n   l i s t [ i ] ,   l i s t [ i + 1 ] ,      ,   l i s t [ j ] 
 
 
 
 e x c e p t   t h a t   t h e   a b o v e   c o d e   c a n   b e   w r i t t e n   o n l y   f o r   a   f i x e d   n u m b e r   o f   e l e m e n t s .   B y   d e f a u l t ,   i   i s   1   a n d   j   i s   t h e   l e n g t h   o f   
 
 t h e   l i s t ,   a s   d e f i n e d   b y   t h e   l e n g t h   o p e r a t o r   ( s e e    2 . 5 . 5 ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 _ V E R S I O N 
 
 A   g l o b a l   v a r i a b l e   ( n o t   a   f u n c t i o n )   t h a t   h o l d s   a   s t r i n g   c o n t a i n i n g   t h e   c u r r e n t   i n t e r p r e t e r   v e r s i o n .   T h e   c u r r e n t   c o n t e n t s   o f   
 
 t h i s   v a r i a b l e   i s   " L u a   5 . 1 " .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 x p c a l l   ( f ,   e r r ) 
 
 T h i s   f u n c t i o n   i s   s i m i l a r   t o   p c a l l ,   e x c e p t   t h a t   y o u   c a n   s e t   a   n e w   e r r o r   h a n d l e r .   
 
 
 
 x p c a l l   c a l l s   f u n c t i o n   f   i n   p r o t e c t e d   m o d e ,   u s i n g   e r r   a s   t h e   e r r o r   h a n d l e r .   A n y   e r r o r   i n s i d e   f   i s   n o t   p r o p a g a t e d ;   i n s t e a d ,   
 
 x p c a l l   c a t c h e s   t h e   e r r o r ,   c a l l s   t h e   e r r   f u n c t i o n   w i t h   t h e   o r i g i n a l   e r r o r   o b j e c t ,   a n d   r e t u r n s   a   s t a t u s   c o d e .   I t s   f i r s t   
 
 r e s u l t   i s   t h e   s t a t u s   c o d e   ( a   b o o l e a n ) ,   w h i c h   i s   t r u e   i f   t h e   c a l l   s u c c e e d s   w i t h o u t   e r r o r s .   I n   t h i s   c a s e ,   x p c a l l   a l s o   r e t u r n s   
 
 a l l   r e s u l t s   f r o m   t h e   c a l l ,   a f t e r   t h i s   f i r s t   r e s u l t .   I n   c a s e   o f   a n y   e r r o r ,   x p c a l l   r e t u r n s   f a l s e   p l u s   t h e   r e s u l t   f r o m   e r r .   
 
 
 
 5 . 2   -   C o r o u t i n e   M a n i p u l a t i o n 
 
 T h e   o p e r a t i o n s   r e l a t e d   t o   c o r o u t i n e s   c o m p r i s e   a   s u b - l i b r a r y   o f   t h e   b a s i c   l i b r a r y   a n d   c o m e   i n s i d e   t h e   t a b l e   c o r o u t i n e .   
 
 S e e    2 . 1 1   f o r   a   g e n e r a l   d e s c r i p t i o n   o f   c o r o u t i n e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . c r e a t e   ( f ) 
 
 C r e a t e s   a   n e w   c o r o u t i n e ,   w i t h   b o d y   f .   f   m u s t   b e   a   L u a   f u n c t i o n .   R e t u r n s   t h i s   n e w   c o r o u t i n e ,   a n   o b j e c t   w i t h   t y p e   " t h r e a d " .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . r e s u m e   ( c o   [ ,   v a l 1 ,      ] ) 
 
 S t a r t s   o r   c o n t i n u e s   t h e   e x e c u t i o n   o f   c o r o u t i n e   c o .   T h e   f i r s t   t i m e   y o u   r e s u m e   a   c o r o u t i n e ,   i t   s t a r t s   r u n n i n g   i t s   b o d y .   T h e   
 
 v a l u e s   v a l 1 ,        a r e   p a s s e d   a s   t h e   a r g u m e n t s   t o   t h e   b o d y   f u n c t i o n .   I f   t h e   c o r o u t i n e   h a s   y i e l d e d ,   r e s u m e   r e s t a r t s   i t ;   t h e   
 
 v a l u e s   v a l 1 ,        a r e   p a s s e d   a s   t h e   r e s u l t s   f r o m   t h e   y i e l d .   
 
 
 
 I f   t h e   c o r o u t i n e   r u n s   w i t h o u t   a n y   e r r o r s ,   r e s u m e   r e t u r n s   t r u e   p l u s   a n y   v a l u e s   p a s s e d   t o   y i e l d   ( i f   t h e   c o r o u t i n e   y i e l d s )   o r   
 
 a n y   v a l u e s   r e t u r n e d   b y   t h e   b o d y   f u n c t i o n   ( i f   t h e   c o r o u t i n e   t e r m i n a t e s ) .   I f   t h e r e   i s   a n y   e r r o r ,   r e s u m e   r e t u r n s   f a l s e   p l u s   
 
 t h e   e r r o r   m e s s a g e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . r u n n i n g   ( ) 
 
 R e t u r n s   t h e   r u n n i n g   c o r o u t i n e ,   o r   n i l   w h e n   c a l l e d   b y   t h e   m a i n   t h r e a d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . s t a t u s   ( c o ) 
 
 R e t u r n s   t h e   s t a t u s   o f   c o r o u t i n e   c o ,   a s   a   s t r i n g :   " r u n n i n g " ,   i f   t h e   c o r o u t i n e   i s   r u n n i n g   ( t h a t   i s ,   i t   c a l l e d   s t a t u s ) ;   
 
 " s u s p e n d e d " ,   i f   t h e   c o r o u t i n e   i s   s u s p e n d e d   i n   a   c a l l   t o   y i e l d ,   o r   i f   i t   h a s   n o t   s t a r t e d   r u n n i n g   y e t ;   " n o r m a l "   i f   t h e   
 
 c o r o u t i n e   i s   a c t i v e   b u t   n o t   r u n n i n g   ( t h a t   i s ,   i t   h a s   r e s u m e d   a n o t h e r   c o r o u t i n e ) ;   a n d   " d e a d "   i f   t h e   c o r o u t i n e   h a s   f i n i s h e d   
 
 i t s   b o d y   f u n c t i o n ,   o r   i f   i t   h a s   s t o p p e d   w i t h   a n   e r r o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . w r a p   ( f ) 
 
 C r e a t e s   a   n e w   c o r o u t i n e ,   w i t h   b o d y   f .   f   m u s t   b e   a   L u a   f u n c t i o n .   R e t u r n s   a   f u n c t i o n   t h a t   r e s u m e s   t h e   c o r o u t i n e   e a c h   t i m e   i t   
 
 i s   c a l l e d .   A n y   a r g u m e n t s   p a s s e d   t o   t h e   f u n c t i o n   b e h a v e   a s   t h e   e x t r a   a r g u m e n t s   t o   r e s u m e .   R e t u r n s   t h e   s a m e   v a l u e s   r e t u r n e d   
 
 b y   r e s u m e ,   e x c e p t   t h e   f i r s t   b o o l e a n .   I n   c a s e   o f   e r r o r ,   p r o p a g a t e s   t h e   e r r o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 c o r o u t i n e . y i e l d   (    ) 
 
 S u s p e n d s   t h e   e x e c u t i o n   o f   t h e   c a l l i n g   c o r o u t i n e .   T h e   c o r o u t i n e   c a n n o t   b e   r u n n i n g   a   C   f u n c t i o n ,   a   m e t a m e t h o d ,   o r   a n   
 
 i t e r a t o r .   A n y   a r g u m e n t s   t o   y i e l d   a r e   p a s s e d   a s   e x t r a   r e s u l t s   t o   r e s u m e .   
 
 
 
 5 . 3   -   M o d u l e s 
 
 T h e   p a c k a g e   l i b r a r y   p r o v i d e s   b a s i c   f a c i l i t i e s   f o r   l o a d i n g   a n d   b u i l d i n g   m o d u l e s   i n   L u a .   I t   e x p o r t s   t w o   o f   i t s   f u n c t i o n s   
 
 d i r e c t l y   i n   t h e   g l o b a l   e n v i r o n m e n t :   r e q u i r e   a n d   m o d u l e .   E v e r y t h i n g   e l s e   i s   e x p o r t e d   i n   a   t a b l e   p a c k a g e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m o d u l e   ( n a m e   [ ,      ] ) 
 
 C r e a t e s   a   m o d u l e .   I f   t h e r e   i s   a   t a b l e   i n   p a c k a g e . l o a d e d [ n a m e ] ,   t h i s   t a b l e   i s   t h e   m o d u l e .   O t h e r w i s e ,   i f   t h e r e   i s   a   g l o b a l   
 
 t a b l e   t   w i t h   t h e   g i v e n   n a m e ,   t h i s   t a b l e   i s   t h e   m o d u l e .   O t h e r w i s e   c r e a t e s   a   n e w   t a b l e   t   a n d   s e t s   i t   a s   t h e   v a l u e   o f   t h e   
 
 g l o b a l   n a m e   a n d   t h e   v a l u e   o f   p a c k a g e . l o a d e d [ n a m e ] .   T h i s   f u n c t i o n   a l s o   i n i t i a l i z e s   t . _ N A M E   w i t h   t h e   g i v e n   n a m e ,   t . _ M   w i t h   
 
 t h e   m o d u l e   ( t   i t s e l f ) ,   a n d   t . _ P A C K A G E   w i t h   t h e   p a c k a g e   n a m e   ( t h e   f u l l   m o d u l e   n a m e   m i n u s   l a s t   c o m p o n e n t ;   s e e   b e l o w ) .   F i n a l l y ,   
 
 m o d u l e   s e t s   t   a s   t h e   n e w   e n v i r o n m e n t   o f   t h e   c u r r e n t   f u n c t i o n   a n d   t h e   n e w   v a l u e   o f   p a c k a g e . l o a d e d [ n a m e ] ,   s o   t h a t   r e q u i r e   
 
 r e t u r n s   t .   
 
 
 
 I f   n a m e   i s   a   c o m p o u n d   n a m e   ( t h a t   i s ,   o n e   w i t h   c o m p o n e n t s   s e p a r a t e d   b y   d o t s ) ,   m o d u l e   c r e a t e s   ( o r   r e u s e s ,   i f   t h e y   a l r e a d y   
 
 e x i s t )   t a b l e s   f o r   e a c h   c o m p o n e n t .   F o r   i n s t a n c e ,   i f   n a m e   i s   a . b . c ,   t h e n   m o d u l e   s t o r e s   t h e   m o d u l e   t a b l e   i n   f i e l d   c   o f   f i e l d   
 
 b   o f   g l o b a l   a .   
 
 
 
 T h i s   f u n c t i o n   c a n   r e c e i v e   o p t i o n a l   o p t i o n s   a f t e r   t h e   m o d u l e   n a m e ,   w h e r e   e a c h   o p t i o n   i s   a   f u n c t i o n   t o   b e   a p p l i e d   o v e r   t h e   
 
 m o d u l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 r e q u i r e   ( m o d n a m e ) 
 
 L o a d s   t h e   g i v e n   m o d u l e .   T h e   f u n c t i o n   s t a r t s   b y   l o o k i n g   i n t o   t h e   p a c k a g e . l o a d e d   t a b l e   t o   d e t e r m i n e   w h e t h e r   m o d n a m e   i s   
 
 a l r e a d y   l o a d e d .   I f   i t   i s ,   t h e n   r e q u i r e   r e t u r n s   t h e   v a l u e   s t o r e d   a t   p a c k a g e . l o a d e d [ m o d n a m e ] .   O t h e r w i s e ,   i t   t r i e s   t o   f i n d   a   
 
 l o a d e r   f o r   t h e   m o d u l e .   
 
 
 
 T o   f i n d   a   l o a d e r ,   r e q u i r e   i s   g u i d e d   b y   t h e   p a c k a g e . l o a d e r s   a r r a y .   B y   c h a n g i n g   t h i s   a r r a y ,   w e   c a n   c h a n g e   h o w   r e q u i r e   l o o k s   
 
 f o r   a   m o d u l e .   T h e   f o l l o w i n g   e x p l a n a t i o n   i s   b a s e d   o n   t h e   d e f a u l t   c o n f i g u r a t i o n   f o r   p a c k a g e . l o a d e r s .   
 
 
 
 F i r s t   r e q u i r e   q u e r i e s   p a c k a g e . p r e l o a d [ m o d n a m e ] .   I f   i t   h a s   a   v a l u e ,   t h i s   v a l u e   ( w h i c h   s h o u l d   b e   a   f u n c t i o n )   i s   t h e   l o a d e r .   
 
 O t h e r w i s e   r e q u i r e   s e a r c h e s   f o r   a   L u a   l o a d e r   u s i n g   t h e   p a t h   s t o r e d   i n   p a c k a g e . p a t h .   I f   t h a t   a l s o   f a i l s ,   i t   s e a r c h e s   f o r   a   C   
 
 l o a d e r   u s i n g   t h e   p a t h   s t o r e d   i n   p a c k a g e . c p a t h .   I f   t h a t   a l s o   f a i l s ,   i t   t r i e s   a n   a l l - i n - o n e   l o a d e r   ( s e e   p a c k a g e . l o a d e r s ) .   
 
 
 
 O n c e   a   l o a d e r   i s   f o u n d ,   r e q u i r e   c a l l s   t h e   l o a d e r   w i t h   a   s i n g l e   a r g u m e n t ,   m o d n a m e .   I f   t h e   l o a d e r   r e t u r n s   a n y   v a l u e ,   r e q u i r e   
 
 a s s i g n s   t h e   r e t u r n e d   v a l u e   t o   p a c k a g e . l o a d e d [ m o d n a m e ] .   I f   t h e   l o a d e r   r e t u r n s   n o   v a l u e   a n d   h a s   n o t   a s s i g n e d   a n y   v a l u e   t o   
 
 p a c k a g e . l o a d e d [ m o d n a m e ] ,   t h e n   r e q u i r e   a s s i g n s   t r u e   t o   t h i s   e n t r y .   I n   a n y   c a s e ,   r e q u i r e   r e t u r n s   t h e   f i n a l   v a l u e   o f   
 
 p a c k a g e . l o a d e d [ m o d n a m e ] .   
 
 
 
 I f   t h e r e   i s   a n y   e r r o r   l o a d i n g   o r   r u n n i n g   t h e   m o d u l e ,   o r   i f   i t   c a n n o t   f i n d   a n y   l o a d e r   f o r   t h e   m o d u l e ,   t h e n   r e q u i r e   s i g n a l s   
 
 a n   e r r o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . c p a t h 
 
 T h e   p a t h   u s e d   b y   r e q u i r e   t o   s e a r c h   f o r   a   C   l o a d e r .   
 
 
 
 L u a   i n i t i a l i z e s   t h e   C   p a t h   p a c k a g e . c p a t h   i n   t h e   s a m e   w a y   i t   i n i t i a l i z e s   t h e   L u a   p a t h   p a c k a g e . p a t h ,   u s i n g   t h e   e n v i r o n m e n t   
 
 v a r i a b l e   L U A _ C P A T H   o r   a   d e f a u l t   p a t h   d e f i n e d   i n   l u a c o n f . h .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . l o a d e d 
 
 A   t a b l e   u s e d   b y   r e q u i r e   t o   c o n t r o l   w h i c h   m o d u l e s   a r e   a l r e a d y   l o a d e d .   W h e n   y o u   r e q u i r e   a   m o d u l e   m o d n a m e   a n d   
 
 p a c k a g e . l o a d e d [ m o d n a m e ]   i s   n o t   f a l s e ,   r e q u i r e   s i m p l y   r e t u r n s   t h e   v a l u e   s t o r e d   t h e r e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . l o a d e r s 
 
 A   t a b l e   u s e d   b y   r e q u i r e   t o   c o n t r o l   h o w   t o   l o a d   m o d u l e s .   
 
 
 
 E a c h   e n t r y   i n   t h i s   t a b l e   i s   a   s e a r c h e r   f u n c t i o n .   W h e n   l o o k i n g   f o r   a   m o d u l e ,   r e q u i r e   c a l l s   e a c h   o f   t h e s e   s e a r c h e r s   i n   
 
 a s c e n d i n g   o r d e r ,   w i t h   t h e   m o d u l e   n a m e   ( t h e   a r g u m e n t   g i v e n   t o   r e q u i r e )   a s   i t s   s o l e   p a r a m e t e r .   T h e   f u n c t i o n   c a n   r e t u r n   
 
 a n o t h e r   f u n c t i o n   ( t h e   m o d u l e   l o a d e r )   o r   a   s t r i n g   e x p l a i n i n g   w h y   i t   d i d   n o t   f i n d   t h a t   m o d u l e   ( o r   n i l   i f   i t   h a s   n o t h i n g   
 
 t o   s a y ) .   L u a   i n i t i a l i z e s   t h i s   t a b l e   w i t h   f o u r   f u n c t i o n s .   
 
 
 
 T h e   f i r s t   s e a r c h e r   s i m p l y   l o o k s   f o r   a   l o a d e r   i n   t h e   p a c k a g e . p r e l o a d   t a b l e .   
 
 
 
 T h e   s e c o n d   s e a r c h e r   l o o k s   f o r   a   l o a d e r   a s   a   L u a   l i b r a r y ,   u s i n g   t h e   p a t h   s t o r e d   a t   p a c k a g e . p a t h .   A   p a t h   i s   a   s e q u e n c e   o f   
 
 t e m p l a t e s   s e p a r a t e d   b y   s e m i c o l o n s .   F o r   e a c h   t e m p l a t e ,   t h e   s e a r c h e r   w i l l   c h a n g e   e a c h   i n t e r r o g a t i o n   m a r k   i n   t h e   t e m p l a t e   b y   
 
 f i l e n a m e ,   w h i c h   i s   t h e   m o d u l e   n a m e   w i t h   e a c h   d o t   r e p l a c e d   b y   a   " d i r e c t o r y   s e p a r a t o r "   ( s u c h   a s   " / "   i n   U n i x ) ;   t h e n   i t   w i l l   
 
 t r y   t o   o p e n   t h e   r e s u l t i n g   f i l e   n a m e .   S o ,   f o r   i n s t a n c e ,   i f   t h e   L u a   p a t h   i s   t h e   s t r i n g   
 
 
 
           " . / ? . l u a ; . / ? . l c ; / u s r / l o c a l / ? / i n i t . l u a " 
 
 
 
 t h e   s e a r c h   f o r   a   L u a   f i l e   f o r   m o d u l e   f o o   w i l l   t r y   t o   o p e n   t h e   f i l e s   . / f o o . l u a ,   . / f o o . l c ,   a n d   / u s r / l o c a l / f o o / i n i t . l u a ,   i n   
 
 t h a t   o r d e r .   
 
 
 
 T h e   t h i r d   s e a r c h e r   l o o k s   f o r   a   l o a d e r   a s   a   C   l i b r a r y ,   u s i n g   t h e   p a t h   g i v e n   b y   t h e   v a r i a b l e   p a c k a g e . c p a t h .   F o r   i n s t a n c e ,   i f   
 
 t h e   C   p a t h   i s   t h e   s t r i n g   
 
 
 
           " . / ? . s o ; . / ? . d l l ; / u s r / l o c a l / ? / i n i t . s o " 
 
 
 
 t h e   s e a r c h e r   f o r   m o d u l e   f o o   w i l l   t r y   t o   o p e n   t h e   f i l e s   . / f o o . s o ,   . / f o o . d l l ,   a n d   / u s r / l o c a l / f o o / i n i t . s o ,   i n   t h a t   o r d e r .   O n c e   
 
 i t   f i n d s   a   C   l i b r a r y ,   t h i s   s e a r c h e r   f i r s t   u s e s   a   d y n a m i c   l i n k   f a c i l i t y   t o   l i n k   t h e   a p p l i c a t i o n   w i t h   t h e   l i b r a r y .   T h e n   i t   
 
 t r i e s   t o   f i n d   a   C   f u n c t i o n   i n s i d e   t h e   l i b r a r y   t o   b e   u s e d   a s   t h e   l o a d e r .   T h e   n a m e   o f   t h i s   C   f u n c t i o n   i s   t h e   s t r i n g   
 
 " l u a o p e n _ "   c o n c a t e n a t e d   w i t h   a   c o p y   o f   t h e   m o d u l e   n a m e   w h e r e   e a c h   d o t   i s   r e p l a c e d   b y   a n   u n d e r s c o r e .   M o r e o v e r ,   i f   t h e   m o d u l e   
 
 n a m e   h a s   a   h y p h e n ,   i t s   p r e f i x   u p   t o   ( a n d   i n c l u d i n g )   t h e   f i r s t   h y p h e n   i s   r e m o v e d .   F o r   i n s t a n c e ,   i f   t h e   m o d u l e   n a m e   i s   
 
 a . v 1 - b . c ,   t h e   f u n c t i o n   n a m e   w i l l   b e   l u a o p e n _ b _ c .   
 
 
 
 T h e   f o u r t h   s e a r c h e r   t r i e s   a n   a l l - i n - o n e   l o a d e r .   I t   s e a r c h e s   t h e   C   p a t h   f o r   a   l i b r a r y   f o r   t h e   r o o t   n a m e   o f   t h e   g i v e n   m o d u l e .   
 
 F o r   i n s t a n c e ,   w h e n   r e q u i r i n g   a . b . c ,   i t   w i l l   s e a r c h   f o r   a   C   l i b r a r y   f o r   a .   I f   f o u n d ,   i t   l o o k s   i n t o   i t   f o r   a n   o p e n   f u n c t i o n   
 
 f o r   t h e   s u b m o d u l e ;   i n   o u r   e x a m p l e ,   t h a t   w o u l d   b e   l u a o p e n _ a _ b _ c .   W i t h   t h i s   f a c i l i t y ,   a   p a c k a g e   c a n   p a c k   s e v e r a l   C   s u b m o d u l e s   
 
 i n t o   o n e   s i n g l e   l i b r a r y ,   w i t h   e a c h   s u b m o d u l e   k e e p i n g   i t s   o r i g i n a l   o p e n   f u n c t i o n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . l o a d l i b   ( l i b n a m e ,   f u n c n a m e ) 
 
 D y n a m i c a l l y   l i n k s   t h e   h o s t   p r o g r a m   w i t h   t h e   C   l i b r a r y   l i b n a m e .   I n s i d e   t h i s   l i b r a r y ,   l o o k s   f o r   a   f u n c t i o n   f u n c n a m e   a n d   
 
 r e t u r n s   t h i s   f u n c t i o n   a s   a   C   f u n c t i o n .   ( S o ,   f u n c n a m e   m u s t   f o l l o w   t h e   p r o t o c o l   ( s e e   l u a _ C F u n c t i o n ) ) .   
 
 
 
 T h i s   i s   a   l o w - l e v e l   f u n c t i o n .   I t   c o m p l e t e l y   b y p a s s e s   t h e   p a c k a g e   a n d   m o d u l e   s y s t e m .   U n l i k e   r e q u i r e ,   i t   d o e s   n o t   p e r f o r m   a n y   
 
 p a t h   s e a r c h i n g   a n d   d o e s   n o t   a u t o m a t i c a l l y   a d d s   e x t e n s i o n s .   l i b n a m e   m u s t   b e   t h e   c o m p l e t e   f i l e   n a m e   o f   t h e   C   l i b r a r y ,   
 
 i n c l u d i n g   i f   n e c e s s a r y   a   p a t h   a n d   e x t e n s i o n .   f u n c n a m e   m u s t   b e   t h e   e x a c t   n a m e   e x p o r t e d   b y   t h e   C   l i b r a r y   ( w h i c h   m a y   d e p e n d   o n   
 
 t h e   C   c o m p i l e r   a n d   l i n k e r   u s e d ) .   
 
 
 
 T h i s   f u n c t i o n   i s   n o t   s u p p o r t e d   b y   A N S I   C .   A s   s u c h ,   i t   i s   o n l y   a v a i l a b l e   o n   s o m e   p l a t f o r m s   ( W i n d o w s ,   L i n u x ,   M a c   O S   X ,   
 
 S o l a r i s ,   B S D ,   p l u s   o t h e r   U n i x   s y s t e m s   t h a t   s u p p o r t   t h e   d l f c n   s t a n d a r d ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . p a t h 
 
 T h e   p a t h   u s e d   b y   r e q u i r e   t o   s e a r c h   f o r   a   L u a   l o a d e r .   
 
 
 
 A t   s t a r t - u p ,   L u a   i n i t i a l i z e s   t h i s   v a r i a b l e   w i t h   t h e   v a l u e   o f   t h e   e n v i r o n m e n t   v a r i a b l e   L U A _ P A T H   o r   w i t h   a   d e f a u l t   p a t h   
 
 d e f i n e d   i n   l u a c o n f . h ,   i f   t h e   e n v i r o n m e n t   v a r i a b l e   i s   n o t   d e f i n e d .   A n y   " ; ; "   i n   t h e   v a l u e   o f   t h e   e n v i r o n m e n t   v a r i a b l e   i s   
 
 r e p l a c e d   b y   t h e   d e f a u l t   p a t h .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . p r e l o a d 
 
 A   t a b l e   t o   s t o r e   l o a d e r s   f o r   s p e c i f i c   m o d u l e s   ( s e e   r e q u i r e ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 p a c k a g e . s e e a l l   ( m o d u l e ) 
 
 S e t s   a   m e t a t a b l e   f o r   m o d u l e   w i t h   i t s   _ _ i n d e x   f i e l d   r e f e r r i n g   t o   t h e   g l o b a l   e n v i r o n m e n t ,   s o   t h a t   t h i s   m o d u l e   i n h e r i t s   v a l u e s   
 
 f r o m   t h e   g l o b a l   e n v i r o n m e n t .   T o   b e   u s e d   a s   a n   o p t i o n   t o   f u n c t i o n   m o d u l e .   
 
 
 
 5 . 4   -   S t r i n g   M a n i p u l a t i o n 
 
 T h i s   l i b r a r y   p r o v i d e s   g e n e r i c   f u n c t i o n s   f o r   s t r i n g   m a n i p u l a t i o n ,   s u c h   a s   f i n d i n g   a n d   e x t r a c t i n g   s u b s t r i n g s ,   a n d   p a t t e r n   
 
 m a t c h i n g .   W h e n   i n d e x i n g   a   s t r i n g   i n   L u a ,   t h e   f i r s t   c h a r a c t e r   i s   a t   p o s i t i o n   1   ( n o t   a t   0 ,   a s   i n   C ) .   I n d i c e s   a r e   a l l o w e d   t o   
 
 b e   n e g a t i v e   a n d   a r e   i n t e r p r e t e d   a s   i n d e x i n g   b a c k w a r d s ,   f r o m   t h e   e n d   o f   t h e   s t r i n g .   T h u s ,   t h e   l a s t   c h a r a c t e r   i s   a t   
 
 p o s i t i o n   - 1 ,   a n d   s o   o n .   
 
 
 
 T h e   s t r i n g   l i b r a r y   p r o v i d e s   a l l   i t s   f u n c t i o n s   i n s i d e   t h e   t a b l e   s t r i n g .   I t   a l s o   s e t s   a   m e t a t a b l e   f o r   s t r i n g s   w h e r e   
 
 t h e   _ _ i n d e x   f i e l d   p o i n t s   t o   t h e   s t r i n g   t a b l e .   T h e r e f o r e ,   y o u   c a n   u s e   t h e   s t r i n g   f u n c t i o n s   i n   o b j e c t - o r i e n t e d   s t y l e .   F o r   
 
 i n s t a n c e ,   s t r i n g . b y t e ( s ,   i )   c a n   b e   w r i t t e n   a s   s : b y t e ( i ) .   
 
 
 
 T h e   s t r i n g   l i b r a r y   a s s u m e s   o n e - b y t e   c h a r a c t e r   e n c o d i n g s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . b y t e   ( s   [ ,   i   [ ,   j ] ] ) 
 
 R e t u r n s   t h e   i n t e r n a l   n u m e r i c a l   c o d e s   o f   t h e   c h a r a c t e r s   s [ i ] ,   s [ i + 1 ] ,      ,   s [ j ] .   T h e   d e f a u l t   v a l u e   f o r   i   i s   1 ;   t h e   d e f a u l t   
 
 v a l u e   f o r   j   i s   i .   
 
 N o t e   t h a t   n u m e r i c a l   c o d e s   a r e   n o t   n e c e s s a r i l y   p o r t a b l e   a c r o s s   p l a t f o r m s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . c h a r   (    ) 
 
 R e c e i v e s   z e r o   o r   m o r e   i n t e g e r s .   R e t u r n s   a   s t r i n g   w i t h   l e n g t h   e q u a l   t o   t h e   n u m b e r   o f   a r g u m e n t s ,   i n   w h i c h   e a c h   c h a r a c t e r   h a s   
 
 t h e   i n t e r n a l   n u m e r i c a l   c o d e   e q u a l   t o   i t s   c o r r e s p o n d i n g   a r g u m e n t .   
 
 N o t e   t h a t   n u m e r i c a l   c o d e s   a r e   n o t   n e c e s s a r i l y   p o r t a b l e   a c r o s s   p l a t f o r m s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . d u m p   ( f u n c t i o n ) 
 
 R e t u r n s   a   s t r i n g   c o n t a i n i n g   a   b i n a r y   r e p r e s e n t a t i o n   o f   t h e   g i v e n   f u n c t i o n ,   s o   t h a t   a   l a t e r   l o a d s t r i n g   o n   t h i s   s t r i n g   
 
 r e t u r n s   a   c o p y   o f   t h e   f u n c t i o n .   f u n c t i o n   m u s t   b e   a   L u a   f u n c t i o n   w i t h o u t   u p v a l u e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . f i n d   ( s ,   p a t t e r n   [ ,   i n i t   [ ,   p l a i n ] ] ) 
 
 L o o k s   f o r   t h e   f i r s t   m a t c h   o f   p a t t e r n   i n   t h e   s t r i n g   s .   I f   i t   f i n d s   a   m a t c h ,   t h e n   f i n d   r e t u r n s   t h e   i n d i c e s   o f   s   w h e r e   t h i s   
 
 o c c u r r e n c e   s t a r t s   a n d   e n d s ;   o t h e r w i s e ,   i t   r e t u r n s   n i l .   A   t h i r d ,   o p t i o n a l   n u m e r i c a l   a r g u m e n t   i n i t   s p e c i f i e s   w h e r e   t o   s t a r t   
 
 t h e   s e a r c h ;   i t s   d e f a u l t   v a l u e   i s   1   a n d   c a n   b e   n e g a t i v e .   A   v a l u e   o f   t r u e   a s   a   f o u r t h ,   o p t i o n a l   a r g u m e n t   p l a i n   t u r n s   o f f   t h e   
 
 p a t t e r n   m a t c h i n g   f a c i l i t i e s ,   s o   t h e   f u n c t i o n   d o e s   a   p l a i n   " f i n d   s u b s t r i n g "   o p e r a t i o n ,   w i t h   n o   c h a r a c t e r s   i n   p a t t e r n   b e i n g   
 
 c o n s i d e r e d   " m a g i c " .   N o t e   t h a t   i f   p l a i n   i s   g i v e n ,   t h e n   i n i t   m u s t   b e   g i v e n   a s   w e l l .   
 
 I f   t h e   p a t t e r n   h a s   c a p t u r e s ,   t h e n   i n   a   s u c c e s s f u l   m a t c h   t h e   c a p t u r e d   v a l u e s   a r e   a l s o   r e t u r n e d ,   a f t e r   t h e   t w o   i n d i c e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . f o r m a t   ( f o r m a t s t r i n g ,      ) 
 
 R e t u r n s   a   f o r m a t t e d   v e r s i o n   o f   i t s   v a r i a b l e   n u m b e r   o f   a r g u m e n t s   f o l l o w i n g   t h e   d e s c r i p t i o n   g i v e n   i n   i t s   f i r s t   a r g u m e n t   
 
 ( w h i c h   m u s t   b e   a   s t r i n g ) .   T h e   f o r m a t   s t r i n g   f o l l o w s   t h e   s a m e   r u l e s   a s   t h e   p r i n t f   f a m i l y   o f   s t a n d a r d   C   f u n c t i o n s .   T h e   o n l y   
 
 d i f f e r e n c e s   a r e   t h a t   t h e   o p t i o n s / m o d i f i e r s   * ,   l ,   L ,   n ,   p ,   a n d   h   a r e   n o t   s u p p o r t e d   a n d   t h a t   t h e r e   i s   a n   e x t r a   o p t i o n ,   q .   T h e   
 
 q   o p t i o n   f o r m a t s   a   s t r i n g   i n   a   f o r m   s u i t a b l e   t o   b e   s a f e l y   r e a d   b a c k   b y   t h e   L u a   i n t e r p r e t e r :   t h e   s t r i n g   i s   w r i t t e n   b e t w e e n   
 
 d o u b l e   q u o t e s ,   a n d   a l l   d o u b l e   q u o t e s ,   n e w l i n e s ,   e m b e d d e d   z e r o s ,   a n d   b a c k s l a s h e s   i n   t h e   s t r i n g   a r e   c o r r e c t l y   e s c a p e d   w h e n   
 
 w r i t t e n .   F o r   i n s t a n c e ,   t h e   c a l l   
 
           s t r i n g . f o r m a t ( ' % q ' ,   ' a   s t r i n g   w i t h   " q u o t e s "   a n d   \ n   n e w   l i n e ' ) 
 
 
 
 w i l l   p r o d u c e   t h e   s t r i n g :   
 
 
 
           " a   s t r i n g   w i t h   \ " q u o t e s \ "   a n d   \ 
 
             n e w   l i n e " 
 
 
 
 T h e   o p t i o n s   c ,   d ,   E ,   e ,   f ,   g ,   G ,   i ,   o ,   u ,   X ,   a n d   x   a l l   e x p e c t   a   n u m b e r   a s   a r g u m e n t ,   w h e r e a s   q   a n d   s   e x p e c t   a   s t r i n g .   
 
 
 
 T h i s   f u n c t i o n   d o e s   n o t   a c c e p t   s t r i n g   v a l u e s   c o n t a i n i n g   e m b e d d e d   z e r o s ,   e x c e p t   a s   a r g u m e n t s   t o   t h e   q   o p t i o n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . g m a t c h   ( s ,   p a t t e r n ) 
 
 R e t u r n s   a n   i t e r a t o r   f u n c t i o n   t h a t ,   e a c h   t i m e   i t   i s   c a l l e d ,   r e t u r n s   t h e   n e x t   c a p t u r e s   f r o m   p a t t e r n   o v e r   s t r i n g   s .   I f   
 
 p a t t e r n   s p e c i f i e s   n o   c a p t u r e s ,   t h e n   t h e   w h o l e   m a t c h   i s   p r o d u c e d   i n   e a c h   c a l l .   
 
 A s   a n   e x a m p l e ,   t h e   f o l l o w i n g   l o o p   
 
 
 
           s   =   " h e l l o   w o r l d   f r o m   L u a " 
 
           f o r   w   i n   s t r i n g . g m a t c h ( s ,   " % a + " )   d o 
 
               p r i n t ( w ) 
 
           e n d 
 
 
 
 w i l l   i t e r a t e   o v e r   a l l   t h e   w o r d s   f r o m   s t r i n g   s ,   p r i n t i n g   o n e   p e r   l i n e .   T h e   n e x t   e x a m p l e   c o l l e c t s   a l l   p a i r s   k e y = v a l u e   f r o m   
 
 t h e   g i v e n   s t r i n g   i n t o   a   t a b l e :   
 
 
 
           t   =   { } 
 
           s   =   " f r o m = w o r l d ,   t o = L u a " 
 
           f o r   k ,   v   i n   s t r i n g . g m a t c h ( s ,   " ( % w + ) = ( % w + ) " )   d o 
 
               t [ k ]   =   v 
 
           e n d 
 
 
 
 F o r   t h i s   f u n c t i o n ,   a   ' ^ '   a t   t h e   s t a r t   o f   a   p a t t e r n   d o e s   n o t   w o r k   a s   a n   a n c h o r ,   a s   t h i s   w o u l d   p r e v e n t   t h e   i t e r a t i o n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . g s u b   ( s ,   p a t t e r n ,   r e p l   [ ,   n ] ) 
 
 R e t u r n s   a   c o p y   o f   s   i n   w h i c h   a l l   ( o r   t h e   f i r s t   n ,   i f   g i v e n )   o c c u r r e n c e s   o f   t h e   p a t t e r n   h a v e   b e e n   r e p l a c e d   b y   a   r e p l a c e m e n t   
 
 s t r i n g   s p e c i f i e d   b y   r e p l ,   w h i c h   c a n   b e   a   s t r i n g ,   a   t a b l e ,   o r   a   f u n c t i o n .   g s u b   a l s o   r e t u r n s ,   a s   i t s   s e c o n d   v a l u e ,   t h e   t o t a l   
 
 n u m b e r   o f   m a t c h e s   t h a t   o c c u r r e d .   
 
 I f   r e p l   i s   a   s t r i n g ,   t h e n   i t s   v a l u e   i s   u s e d   f o r   r e p l a c e m e n t .   T h e   c h a r a c t e r   %   w o r k s   a s   a n   e s c a p e   c h a r a c t e r :   a n y   s e q u e n c e   i n   
 
 r e p l   o f   t h e   f o r m   % n ,   w i t h   n   b e t w e e n   1   a n d   9 ,   s t a n d s   f o r   t h e   v a l u e   o f   t h e   n - t h   c a p t u r e d   s u b s t r i n g   ( s e e   b e l o w ) .   T h e   s e q u e n c e   
 
 % 0   s t a n d s   f o r   t h e   w h o l e   m a t c h .   T h e   s e q u e n c e   % %   s t a n d s   f o r   a   s i n g l e   % .   
 
 
 
 I f   r e p l   i s   a   t a b l e ,   t h e n   t h e   t a b l e   i s   q u e r i e d   f o r   e v e r y   m a t c h ,   u s i n g   t h e   f i r s t   c a p t u r e   a s   t h e   k e y ;   i f   t h e   p a t t e r n   s p e c i f i e s   
 
 n o   c a p t u r e s ,   t h e n   t h e   w h o l e   m a t c h   i s   u s e d   a s   t h e   k e y .   
 
 
 
 I f   r e p l   i s   a   f u n c t i o n ,   t h e n   t h i s   f u n c t i o n   i s   c a l l e d   e v e r y   t i m e   a   m a t c h   o c c u r s ,   w i t h   a l l   c a p t u r e d   s u b s t r i n g s   p a s s e d   a s   
 
 a r g u m e n t s ,   i n   o r d e r ;   i f   t h e   p a t t e r n   s p e c i f i e s   n o   c a p t u r e s ,   t h e n   t h e   w h o l e   m a t c h   i s   p a s s e d   a s   a   s o l e   a r g u m e n t .   
 
 
 
 I f   t h e   v a l u e   r e t u r n e d   b y   t h e   t a b l e   q u e r y   o r   b y   t h e   f u n c t i o n   c a l l   i s   a   s t r i n g   o r   a   n u m b e r ,   t h e n   i t   i s   u s e d   a s   t h e   
 
 r e p l a c e m e n t   s t r i n g ;   o t h e r w i s e ,   i f   i t   i s   f a l s e   o r   n i l ,   t h e n   t h e r e   i s   n o   r e p l a c e m e n t   ( t h a t   i s ,   t h e   o r i g i n a l   m a t c h   i s   k e p t   i n   
 
 t h e   s t r i n g ) .   
 
 
 
 H e r e   a r e   s o m e   e x a m p l e s :   
 
 
 
           x   =   s t r i n g . g s u b ( " h e l l o   w o r l d " ,   " ( % w + ) " ,   " % 1   % 1 " ) 
 
           - - >   x = " h e l l o   h e l l o   w o r l d   w o r l d " 
 
           
 
           x   =   s t r i n g . g s u b ( " h e l l o   w o r l d " ,   " % w + " ,   " % 0   % 0 " ,   1 ) 
 
           - - >   x = " h e l l o   h e l l o   w o r l d " 
 
           
 
           x   =   s t r i n g . g s u b ( " h e l l o   w o r l d   f r o m   L u a " ,   " ( % w + ) % s * ( % w + ) " ,   " % 2   % 1 " ) 
 
           - - >   x = " w o r l d   h e l l o   L u a   f r o m " 
 
           
 
           x   =   s t r i n g . g s u b ( " h o m e   =   $ H O M E ,   u s e r   =   $ U S E R " ,   " % $ ( % w + ) " ,   o s . g e t e n v ) 
 
           - - >   x = " h o m e   =   / h o m e / r o b e r t o ,   u s e r   =   r o b e r t o " 
 
           
 
           x   =   s t r i n g . g s u b ( " 4 + 5   =   $ r e t u r n   4 + 5 $ " ,   " % $ ( . - ) % $ " ,   f u n c t i o n   ( s ) 
 
                       r e t u r n   l o a d s t r i n g ( s ) ( ) 
 
                   e n d ) 
 
           - - >   x = " 4 + 5   =   9 " 
 
           
 
           l o c a l   t   =   { n a m e = " l u a " ,   v e r s i o n = " 5 . 1 " } 
 
           x   =   s t r i n g . g s u b ( " $ n a m e - $ v e r s i o n . t a r . g z " ,   " % $ ( % w + ) " ,   t ) 
 
           - - >   x = " l u a - 5 . 1 . t a r . g z " 
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . l e n   ( s ) 
 
 R e c e i v e s   a   s t r i n g   a n d   r e t u r n s   i t s   l e n g t h .   T h e   e m p t y   s t r i n g   " "   h a s   l e n g t h   0 .   E m b e d d e d   z e r o s   a r e   c o u n t e d ,   s o   " a \ 0 0 0 b c \ 0 0 0 "   
 
 h a s   l e n g t h   5 .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . l o w e r   ( s ) 
 
 R e c e i v e s   a   s t r i n g   a n d   r e t u r n s   a   c o p y   o f   t h i s   s t r i n g   w i t h   a l l   u p p e r c a s e   l e t t e r s   c h a n g e d   t o   l o w e r c a s e .   A l l   o t h e r   c h a r a c t e r s   
 
 a r e   l e f t   u n c h a n g e d .   T h e   d e f i n i t i o n   o f   w h a t   a n   u p p e r c a s e   l e t t e r   i s   d e p e n d s   o n   t h e   c u r r e n t   l o c a l e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . m a t c h   ( s ,   p a t t e r n   [ ,   i n i t ] ) 
 
 L o o k s   f o r   t h e   f i r s t   m a t c h   o f   p a t t e r n   i n   t h e   s t r i n g   s .   I f   i t   f i n d s   o n e ,   t h e n   m a t c h   r e t u r n s   t h e   c a p t u r e s   f r o m   t h e   p a t t e r n ;   
 
 o t h e r w i s e   i t   r e t u r n s   n i l .   I f   p a t t e r n   s p e c i f i e s   n o   c a p t u r e s ,   t h e n   t h e   w h o l e   m a t c h   i s   r e t u r n e d .   A   t h i r d ,   o p t i o n a l   n u m e r i c a l   
 
 a r g u m e n t   i n i t   s p e c i f i e s   w h e r e   t o   s t a r t   t h e   s e a r c h ;   i t s   d e f a u l t   v a l u e   i s   1   a n d   c a n   b e   n e g a t i v e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . r e p   ( s ,   n ) 
 
 R e t u r n s   a   s t r i n g   t h a t   i s   t h e   c o n c a t e n a t i o n   o f   n   c o p i e s   o f   t h e   s t r i n g   s .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . r e v e r s e   ( s ) 
 
 R e t u r n s   a   s t r i n g   t h a t   i s   t h e   s t r i n g   s   r e v e r s e d .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . s u b   ( s ,   i   [ ,   j ] ) 
 
 R e t u r n s   t h e   s u b s t r i n g   o f   s   t h a t   s t a r t s   a t   i   a n d   c o n t i n u e s   u n t i l   j ;   i   a n d   j   c a n   b e   n e g a t i v e .   I f   j   i s   a b s e n t ,   t h e n   i t   i s   
 
 a s s u m e d   t o   b e   e q u a l   t o   - 1   ( w h i c h   i s   t h e   s a m e   a s   t h e   s t r i n g   l e n g t h ) .   I n   p a r t i c u l a r ,   t h e   c a l l   s t r i n g . s u b ( s , 1 , j )   r e t u r n s   a   
 
 p r e f i x   o f   s   w i t h   l e n g t h   j ,   a n d   s t r i n g . s u b ( s ,   - i )   r e t u r n s   a   s u f f i x   o f   s   w i t h   l e n g t h   i .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 s t r i n g . u p p e r   ( s ) 
 
 R e c e i v e s   a   s t r i n g   a n d   r e t u r n s   a   c o p y   o f   t h i s   s t r i n g   w i t h   a l l   l o w e r c a s e   l e t t e r s   c h a n g e d   t o   u p p e r c a s e .   A l l   o t h e r   c h a r a c t e r s   
 
 a r e   l e f t   u n c h a n g e d .   T h e   d e f i n i t i o n   o f   w h a t   a   l o w e r c a s e   l e t t e r   i s   d e p e n d s   o n   t h e   c u r r e n t   l o c a l e .   
 
 5 . 4 . 1   -   P a t t e r n s 
 
 C h a r a c t e r   C l a s s : 
 
 A   c h a r a c t e r   c l a s s   i s   u s e d   t o   r e p r e s e n t   a   s e t   o f   c h a r a c t e r s .   T h e   f o l l o w i n g   c o m b i n a t i o n s   a r e   a l l o w e d   i n   d e s c r i b i n g   a   
 
 c h a r a c t e r   c l a s s :   
 
 
 
 x :   ( w h e r e   x   i s   n o t   o n e   o f   t h e   m a g i c   c h a r a c t e r s   ^ $ ( ) % . [ ] * + - ? )   r e p r e s e n t s   t h e   c h a r a c t e r   x   i t s e l f .   
 
 . :   ( a   d o t )   r e p r e s e n t s   a l l   c h a r a c t e r s .   
 
 % a :   r e p r e s e n t s   a l l   l e t t e r s .   
 
 % c :   r e p r e s e n t s   a l l   c o n t r o l   c h a r a c t e r s .   
 
 % d :   r e p r e s e n t s   a l l   d i g i t s .   
 
 % l :   r e p r e s e n t s   a l l   l o w e r c a s e   l e t t e r s .   
 
 % p :   r e p r e s e n t s   a l l   p u n c t u a t i o n   c h a r a c t e r s .   
 
 % s :   r e p r e s e n t s   a l l   s p a c e   c h a r a c t e r s .   
 
 % u :   r e p r e s e n t s   a l l   u p p e r c a s e   l e t t e r s .   
 
 % w :   r e p r e s e n t s   a l l   a l p h a n u m e r i c   c h a r a c t e r s .   
 
 % x :   r e p r e s e n t s   a l l   h e x a d e c i m a l   d i g i t s .   
 
 % z :   r e p r e s e n t s   t h e   c h a r a c t e r   w i t h   r e p r e s e n t a t i o n   0 .   
 
 % x :   ( w h e r e   x   i s   a n y   n o n - a l p h a n u m e r i c   c h a r a c t e r )   r e p r e s e n t s   t h e   c h a r a c t e r   x .   T h i s   i s   t h e   s t a n d a r d   w a y   t o   e s c a p e   t h e   m a g i c   
 
 c h a r a c t e r s .   A n y   p u n c t u a t i o n   c h a r a c t e r   ( e v e n   t h e   n o n   m a g i c )   c a n   b e   p r e c e d e d   b y   a   ' % '   w h e n   u s e d   t o   r e p r e s e n t   i t s e l f   i n   a   
 
 p a t t e r n .   
 
 [ s e t ] :   r e p r e s e n t s   t h e   c l a s s   w h i c h   i s   t h e   u n i o n   o f   a l l   c h a r a c t e r s   i n   s e t .   A   r a n g e   o f   c h a r a c t e r s   c a n   b e   s p e c i f i e d   b y   
 
 s e p a r a t i n g   t h e   e n d   c h a r a c t e r s   o f   t h e   r a n g e   w i t h   a   ' - ' .   A l l   c l a s s e s   % x   d e s c r i b e d   a b o v e   c a n   a l s o   b e   u s e d   a s   c o m p o n e n t s   i n   
 
 s e t .   A l l   o t h e r   c h a r a c t e r s   i n   s e t   r e p r e s e n t   t h e m s e l v e s .   F o r   e x a m p l e ,   [ % w _ ]   ( o r   [ _ % w ] )   r e p r e s e n t s   a l l   a l p h a n u m e r i c   c h a r a c t e r s   
 
 p l u s   t h e   u n d e r s c o r e ,   [ 0 - 7 ]   r e p r e s e n t s   t h e   o c t a l   d i g i t s ,   a n d   [ 0 - 7 % l % - ]   r e p r e s e n t s   t h e   o c t a l   d i g i t s   p l u s   t h e   l o w e r c a s e   
 
 l e t t e r s   p l u s   t h e   ' - '   c h a r a c t e r .   
 
 T h e   i n t e r a c t i o n   b e t w e e n   r a n g e s   a n d   c l a s s e s   i s   n o t   d e f i n e d .   T h e r e f o r e ,   p a t t e r n s   l i k e   [ % a - z ]   o r   [ a - % % ]   h a v e   n o   m e a n i n g .   
 
 
 
 [ ^ s e t ] :   r e p r e s e n t s   t h e   c o m p l e m e n t   o f   s e t ,   w h e r e   s e t   i s   i n t e r p r e t e d   a s   a b o v e .   
 
 F o r   a l l   c l a s s e s   r e p r e s e n t e d   b y   s i n g l e   l e t t e r s   ( % a ,   % c ,   e t c . ) ,   t h e   c o r r e s p o n d i n g   u p p e r c a s e   l e t t e r   r e p r e s e n t s   t h e   c o m p l e m e n t   
 
 o f   t h e   c l a s s .   F o r   i n s t a n c e ,   % S   r e p r e s e n t s   a l l   n o n - s p a c e   c h a r a c t e r s .   
 
 
 
 T h e   d e f i n i t i o n s   o f   l e t t e r ,   s p a c e ,   a n d   o t h e r   c h a r a c t e r   g r o u p s   d e p e n d   o n   t h e   c u r r e n t   l o c a l e .   I n   p a r t i c u l a r ,   t h e   c l a s s   [ a - z ]   
 
 m a y   n o t   b e   e q u i v a l e n t   t o   % l .   
 
 
 
 P a t t e r n   I t e m : 
 
 A   p a t t e r n   i t e m   c a n   b e   
 
 
 
 a   s i n g l e   c h a r a c t e r   c l a s s ,   w h i c h   m a t c h e s   a n y   s i n g l e   c h a r a c t e r   i n   t h e   c l a s s ;   
 
 a   s i n g l e   c h a r a c t e r   c l a s s   f o l l o w e d   b y   ' * ' ,   w h i c h   m a t c h e s   0   o r   m o r e   r e p e t i t i o n s   o f   c h a r a c t e r s   i n   t h e   c l a s s .   T h e s e   r e p e t i t i o n   
 
 i t e m s   w i l l   a l w a y s   m a t c h   t h e   l o n g e s t   p o s s i b l e   s e q u e n c e ;   
 
 a   s i n g l e   c h a r a c t e r   c l a s s   f o l l o w e d   b y   ' + ' ,   w h i c h   m a t c h e s   1   o r   m o r e   r e p e t i t i o n s   o f   c h a r a c t e r s   i n   t h e   c l a s s .   T h e s e   r e p e t i t i o n   
 
 i t e m s   w i l l   a l w a y s   m a t c h   t h e   l o n g e s t   p o s s i b l e   s e q u e n c e ;   
 
 a   s i n g l e   c h a r a c t e r   c l a s s   f o l l o w e d   b y   ' - ' ,   w h i c h   a l s o   m a t c h e s   0   o r   m o r e   r e p e t i t i o n s   o f   c h a r a c t e r s   i n   t h e   c l a s s .   U n l i k e   ' * ' ,   
 
 t h e s e   r e p e t i t i o n   i t e m s   w i l l   a l w a y s   m a t c h   t h e   s h o r t e s t   p o s s i b l e   s e q u e n c e ;   
 
 a   s i n g l e   c h a r a c t e r   c l a s s   f o l l o w e d   b y   ' ? ' ,   w h i c h   m a t c h e s   0   o r   1   o c c u r r e n c e   o f   a   c h a r a c t e r   i n   t h e   c l a s s ;   
 
 % n ,   f o r   n   b e t w e e n   1   a n d   9 ;   s u c h   i t e m   m a t c h e s   a   s u b s t r i n g   e q u a l   t o   t h e   n - t h   c a p t u r e d   s t r i n g   ( s e e   b e l o w ) ;   
 
 % b x y ,   w h e r e   x   a n d   y   a r e   t w o   d i s t i n c t   c h a r a c t e r s ;   s u c h   i t e m   m a t c h e s   s t r i n g s   t h a t   s t a r t   w i t h   x ,   e n d   w i t h   y ,   a n d   w h e r e   t h e   x   
 
 a n d   y   a r e   b a l a n c e d .   T h i s   m e a n s   t h a t ,   i f   o n e   r e a d s   t h e   s t r i n g   f r o m   l e f t   t o   r i g h t ,   c o u n t i n g   + 1   f o r   a n   x   a n d   - 1   f o r   a   y ,   t h e   
 
 e n d i n g   y   i s   t h e   f i r s t   y   w h e r e   t h e   c o u n t   r e a c h e s   0 .   F o r   i n s t a n c e ,   t h e   i t e m   % b ( )   m a t c h e s   e x p r e s s i o n s   w i t h   b a l a n c e d   
 
 p a r e n t h e s e s .   
 
 P a t t e r n : 
 
 A   p a t t e r n   i s   a   s e q u e n c e   o f   p a t t e r n   i t e m s .   A   ' ^ '   a t   t h e   b e g i n n i n g   o f   a   p a t t e r n   a n c h o r s   t h e   m a t c h   a t   t h e   b e g i n n i n g   o f   t h e   
 
 s u b j e c t   s t r i n g .   A   ' $ '   a t   t h e   e n d   o f   a   p a t t e r n   a n c h o r s   t h e   m a t c h   a t   t h e   e n d   o f   t h e   s u b j e c t   s t r i n g .   A t   o t h e r   p o s i t i o n s ,   ' ^ '   
 
 a n d   ' $ '   h a v e   n o   s p e c i a l   m e a n i n g   a n d   r e p r e s e n t   t h e m s e l v e s .   
 
 
 
 C a p t u r e s : 
 
 A   p a t t e r n   c a n   c o n t a i n   s u b - p a t t e r n s   e n c l o s e d   i n   p a r e n t h e s e s ;   t h e y   d e s c r i b e   c a p t u r e s .   W h e n   a   m a t c h   s u c c e e d s ,   t h e   s u b s t r i n g s   
 
 o f   t h e   s u b j e c t   s t r i n g   t h a t   m a t c h   c a p t u r e s   a r e   s t o r e d   ( c a p t u r e d )   f o r   f u t u r e   u s e .   C a p t u r e s   a r e   n u m b e r e d   a c c o r d i n g   t o   t h e i r   
 
 l e f t   p a r e n t h e s e s .   F o r   i n s t a n c e ,   i n   t h e   p a t t e r n   " ( a * ( . ) % w ( % s * ) ) " ,   t h e   p a r t   o f   t h e   s t r i n g   m a t c h i n g   " a * ( . ) % w ( % s * ) "   i s   s t o r e d   
 
 a s   t h e   f i r s t   c a p t u r e   ( a n d   t h e r e f o r e   h a s   n u m b e r   1 ) ;   t h e   c h a r a c t e r   m a t c h i n g   " . "   i s   c a p t u r e d   w i t h   n u m b e r   2 ,   a n d   t h e   p a r t   
 
 m a t c h i n g   " % s * "   h a s   n u m b e r   3 .   
 
 
 
 A s   a   s p e c i a l   c a s e ,   t h e   e m p t y   c a p t u r e   ( )   c a p t u r e s   t h e   c u r r e n t   s t r i n g   p o s i t i o n   ( a   n u m b e r ) .   F o r   i n s t a n c e ,   i f   w e   a p p l y   t h e   
 
 p a t t e r n   " ( ) a a ( ) "   o n   t h e   s t r i n g   " f l a a a p " ,   t h e r e   w i l l   b e   t w o   c a p t u r e s :   3   a n d   5 .   
 
 
 
 A   p a t t e r n   c a n n o t   c o n t a i n   e m b e d d e d   z e r o s .   U s e   % z   i n s t e a d .   
 
 
 
 5 . 5   -   T a b l e   M a n i p u l a t i o n 
 
 T h i s   l i b r a r y   p r o v i d e s   g e n e r i c   f u n c t i o n s   f o r   t a b l e   m a n i p u l a t i o n .   I t   p r o v i d e s   a l l   i t s   f u n c t i o n s   i n s i d e   t h e   t a b l e   t a b l e .   
 
 
 
 M o s t   f u n c t i o n s   i n   t h e   t a b l e   l i b r a r y   a s s u m e   t h a t   t h e   t a b l e   r e p r e s e n t s   a n   a r r a y   o r   a   l i s t .   F o r   t h e s e   f u n c t i o n s ,   w h e n   w e   t a l k   
 
 a b o u t   t h e   " l e n g t h "   o f   a   t a b l e   w e   m e a n   t h e   r e s u l t   o f   t h e   l e n g t h   o p e r a t o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t a b l e . c o n c a t   ( t a b l e   [ ,   s e p   [ ,   i   [ ,   j ] ] ] ) 
 
 G i v e n   a n   a r r a y   w h e r e   a l l   e l e m e n t s   a r e   s t r i n g s   o r   n u m b e r s ,   r e t u r n s   t a b l e [ i ] . . s e p . . t a b l e [ i + 1 ]        s e p . . t a b l e [ j ] .   T h e   d e f a u l t   
 
 v a l u e   f o r   s e p   i s   t h e   e m p t y   s t r i n g ,   t h e   d e f a u l t   f o r   i   i s   1 ,   a n d   t h e   d e f a u l t   f o r   j   i s   t h e   l e n g t h   o f   t h e   t a b l e .   I f   i   i s   
 
 g r e a t e r   t h a n   j ,   r e t u r n s   t h e   e m p t y   s t r i n g .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t a b l e . i n s e r t   ( t a b l e ,   [ p o s , ]   v a l u e ) 
 
 I n s e r t s   e l e m e n t   v a l u e   a t   p o s i t i o n   p o s   i n   t a b l e ,   s h i f t i n g   u p   o t h e r   e l e m e n t s   t o   o p e n   s p a c e ,   i f   n e c e s s a r y .   T h e   d e f a u l t   v a l u e   
 
 f o r   p o s   i s   n + 1 ,   w h e r e   n   i s   t h e   l e n g t h   o f   t h e   t a b l e   ( s e e    2 . 5 . 5 ) ,   s o   t h a t   a   c a l l   t a b l e . i n s e r t ( t , x )   i n s e r t s   x   a t   t h e   e n d   o f   
 
 t a b l e   t .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t a b l e . m a x n   ( t a b l e ) 
 
 R e t u r n s   t h e   l a r g e s t   p o s i t i v e   n u m e r i c a l   i n d e x   o f   t h e   g i v e n   t a b l e ,   o r   z e r o   i f   t h e   t a b l e   h a s   n o   p o s i t i v e   n u m e r i c a l   i n d i c e s .   
 
 ( T o   d o   i t s   j o b   t h i s   f u n c t i o n   d o e s   a   l i n e a r   t r a v e r s a l   o f   t h e   w h o l e   t a b l e . )   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t a b l e . r e m o v e   ( t a b l e   [ ,   p o s ] ) 
 
 R e m o v e s   f r o m   t a b l e   t h e   e l e m e n t   a t   p o s i t i o n   p o s ,   s h i f t i n g   d o w n   o t h e r   e l e m e n t s   t o   c l o s e   t h e   s p a c e ,   i f   n e c e s s a r y .   R e t u r n s   t h e   
 
 v a l u e   o f   t h e   r e m o v e d   e l e m e n t .   T h e   d e f a u l t   v a l u e   f o r   p o s   i s   n ,   w h e r e   n   i s   t h e   l e n g t h   o f   t h e   t a b l e ,   s o   t h a t   a   c a l l   
 
 t a b l e . r e m o v e ( t )   r e m o v e s   t h e   l a s t   e l e m e n t   o f   t a b l e   t .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 t a b l e . s o r t   ( t a b l e   [ ,   c o m p ] ) 
 
 S o r t s   t a b l e   e l e m e n t s   i n   a   g i v e n   o r d e r ,   i n - p l a c e ,   f r o m   t a b l e [ 1 ]   t o   t a b l e [ n ] ,   w h e r e   n   i s   t h e   l e n g t h   o f   t h e   t a b l e .   I f   c o m p   i s   
 
 g i v e n ,   t h e n   i t   m u s t   b e   a   f u n c t i o n   t h a t   r e c e i v e s   t w o   t a b l e   e l e m e n t s ,   a n d   r e t u r n s   t r u e   w h e n   t h e   f i r s t   i s   l e s s   t h a n   t h e   s e c o n d   
 
 ( s o   t h a t   n o t   c o m p ( a [ i + 1 ] , a [ i ] )   w i l l   b e   t r u e   a f t e r   t h e   s o r t ) .   I f   c o m p   i s   n o t   g i v e n ,   t h e n   t h e   s t a n d a r d   L u a   o p e r a t o r   <   i s   u s e d   
 
 i n s t e a d .   
 
 T h e   s o r t   a l g o r i t h m   i s   n o t   s t a b l e ;   t h a t   i s ,   e l e m e n t s   c o n s i d e r e d   e q u a l   b y   t h e   g i v e n   o r d e r   m a y   h a v e   t h e i r   r e l a t i v e   p o s i t i o n s   
 
 c h a n g e d   b y   t h e   s o r t .   
 
 
 
 5 . 6   -   M a t h e m a t i c a l   F u n c t i o n s 
 
 T h i s   l i b r a r y   i s   a n   i n t e r f a c e   t o   t h e   s t a n d a r d   C   m a t h   l i b r a r y .   I t   p r o v i d e s   a l l   i t s   f u n c t i o n s   i n s i d e   t h e   t a b l e   m a t h .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . a b s   ( x ) 
 
 R e t u r n s   t h e   a b s o l u t e   v a l u e   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . a c o s   ( x ) 
 
 R e t u r n s   t h e   a r c   c o s i n e   o f   x   ( i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . a s i n   ( x ) 
 
 R e t u r n s   t h e   a r c   s i n e   o f   x   ( i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . a t a n   ( x ) 
 
 R e t u r n s   t h e   a r c   t a n g e n t   o f   x   ( i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . a t a n 2   ( y ,   x ) 
 
 R e t u r n s   t h e   a r c   t a n g e n t   o f   y / x   ( i n   r a d i a n s ) ,   b u t   u s e s   t h e   s i g n s   o f   b o t h   p a r a m e t e r s   t o   f i n d   t h e   q u a d r a n t   o f   t h e   r e s u l t .   
 
 ( I t   a l s o   h a n d l e s   c o r r e c t l y   t h e   c a s e   o f   x   b e i n g   z e r o . )   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . c e i l   ( x ) 
 
 R e t u r n s   t h e   s m a l l e s t   i n t e g e r   l a r g e r   t h a n   o r   e q u a l   t o   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . c o s   ( x ) 
 
 R e t u r n s   t h e   c o s i n e   o f   x   ( a s s u m e d   t o   b e   i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . c o s h   ( x ) 
 
 R e t u r n s   t h e   h y p e r b o l i c   c o s i n e   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . d e g   ( x ) 
 
 R e t u r n s   t h e   a n g l e   x   ( g i v e n   i n   r a d i a n s )   i n   d e g r e e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . e x p   ( x ) 
 
 R e t u r n s   t h e   v a l u e   e x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . f l o o r   ( x ) 
 
 R e t u r n s   t h e   l a r g e s t   i n t e g e r   s m a l l e r   t h a n   o r   e q u a l   t o   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . f m o d   ( x ,   y ) 
 
 R e t u r n s   t h e   r e m a i n d e r   o f   t h e   d i v i s i o n   o f   x   b y   y   t h a t   r o u n d s   t h e   q u o t i e n t   t o w a r d s   z e r o .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . f r e x p   ( x ) 
 
 R e t u r n s   m   a n d   e   s u c h   t h a t   x   =   m 2 e ,   e   i s   a n   i n t e g e r   a n d   t h e   a b s o l u t e   v a l u e   o f   m   i s   i n   t h e   r a n g e   [ 0 . 5 ,   1 )   ( o r   z e r o   w h e n   x   i s   
 
 z e r o ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . h u g e 
 
 T h e   v a l u e   H U G E _ V A L ,   a   v a l u e   l a r g e r   t h a n   o r   e q u a l   t o   a n y   o t h e r   n u m e r i c a l   v a l u e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . l d e x p   ( m ,   e ) 
 
 R e t u r n s   m 2 e   ( e   s h o u l d   b e   a n   i n t e g e r ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . l o g   ( x ) 
 
 R e t u r n s   t h e   n a t u r a l   l o g a r i t h m   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . l o g 1 0   ( x ) 
 
 R e t u r n s   t h e   b a s e - 1 0   l o g a r i t h m   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . m a x   ( x ,      ) 
 
 R e t u r n s   t h e   m a x i m u m   v a l u e   a m o n g   i t s   a r g u m e n t s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . m i n   ( x ,      ) 
 
 R e t u r n s   t h e   m i n i m u m   v a l u e   a m o n g   i t s   a r g u m e n t s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . m o d f   ( x ) 
 
 R e t u r n s   t w o   n u m b e r s ,   t h e   i n t e g r a l   p a r t   o f   x   a n d   t h e   f r a c t i o n a l   p a r t   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . p i 
 
 T h e   v a l u e   o f   p i .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . p o w   ( x ,   y ) 
 
 R e t u r n s   x y .   ( Y o u   c a n   a l s o   u s e   t h e   e x p r e s s i o n   x ^ y   t o   c o m p u t e   t h i s   v a l u e . )   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . r a d   ( x ) 
 
 R e t u r n s   t h e   a n g l e   x   ( g i v e n   i n   d e g r e e s )   i n   r a d i a n s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . r a n d o m   ( [ m   [ ,   n ] ] ) 
 
 T h i s   f u n c t i o n   i s   a n   i n t e r f a c e   t o   t h e   s i m p l e   p s e u d o - r a n d o m   g e n e r a t o r   f u n c t i o n   r a n d   p r o v i d e d   b y   A N S I   C .   ( N o   g u a r a n t e e s   c a n   b e   
 
 g i v e n   f o r   i t s   s t a t i s t i c a l   p r o p e r t i e s . )   
 
 
 
 W h e n   c a l l e d   w i t h o u t   a r g u m e n t s ,   r e t u r n s   a   u n i f o r m   p s e u d o - r a n d o m   r e a l   n u m b e r   i n   t h e   r a n g e   [ 0 , 1 ) .   W h e n   c a l l e d   w i t h   a n   i n t e g e r   
 
 n u m b e r   m ,   m a t h . r a n d o m   r e t u r n s   a   u n i f o r m   p s e u d o - r a n d o m   i n t e g e r   i n   t h e   r a n g e   [ 1 ,   m ] .   W h e n   c a l l e d   w i t h   t w o   i n t e g e r   n u m b e r s   m   
 
 a n d   n ,   m a t h . r a n d o m   r e t u r n s   a   u n i f o r m   p s e u d o - r a n d o m   i n t e g e r   i n   t h e   r a n g e   [ m ,   n ] .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . r a n d o m s e e d   ( x ) 
 
 S e t s   x   a s   t h e   " s e e d "   f o r   t h e   p s e u d o - r a n d o m   g e n e r a t o r :   e q u a l   s e e d s   p r o d u c e   e q u a l   s e q u e n c e s   o f   n u m b e r s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . s i n   ( x ) 
 
 R e t u r n s   t h e   s i n e   o f   x   ( a s s u m e d   t o   b e   i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . s i n h   ( x ) 
 
 R e t u r n s   t h e   h y p e r b o l i c   s i n e   o f   x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . s q r t   ( x ) 
 
 R e t u r n s   t h e   s q u a r e   r o o t   o f   x .   ( Y o u   c a n   a l s o   u s e   t h e   e x p r e s s i o n   x ^ 0 . 5   t o   c o m p u t e   t h i s   v a l u e . )   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . t a n   ( x ) 
 
 R e t u r n s   t h e   t a n g e n t   o f   x   ( a s s u m e d   t o   b e   i n   r a d i a n s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 m a t h . t a n h   ( x ) 
 
 R e t u r n s   t h e   h y p e r b o l i c   t a n g e n t   o f   x .   
 
 
 
 5 . 7   -   I n p u t   a n d   O u t p u t   F a c i l i t i e s 
 
 T h e   I / O   l i b r a r y   p r o v i d e s   t w o   d i f f e r e n t   s t y l e s   f o r   f i l e   m a n i p u l a t i o n .   T h e   f i r s t   o n e   u s e s   i m p l i c i t   f i l e   d e s c r i p t o r s ;   t h a t   i s ,   
 
 t h e r e   a r e   o p e r a t i o n s   t o   s e t   a   d e f a u l t   i n p u t   f i l e   a n d   a   d e f a u l t   o u t p u t   f i l e ,   a n d   a l l   i n p u t / o u t p u t   o p e r a t i o n s   a r e   o v e r   t h e s e   
 
 d e f a u l t   f i l e s .   T h e   s e c o n d   s t y l e   u s e s   e x p l i c i t   f i l e   d e s c r i p t o r s .   
 
 
 
 W h e n   u s i n g   i m p l i c i t   f i l e   d e s c r i p t o r s ,   a l l   o p e r a t i o n s   a r e   s u p p l i e d   b y   t a b l e   i o .   W h e n   u s i n g   e x p l i c i t   f i l e   d e s c r i p t o r s ,   t h e   
 
 o p e r a t i o n   i o . o p e n   r e t u r n s   a   f i l e   d e s c r i p t o r   a n d   t h e n   a l l   o p e r a t i o n s   a r e   s u p p l i e d   a s   m e t h o d s   o f   t h e   f i l e   d e s c r i p t o r .   
 
 
 
 T h e   t a b l e   i o   a l s o   p r o v i d e s   t h r e e   p r e d e f i n e d   f i l e   d e s c r i p t o r s   w i t h   t h e i r   u s u a l   m e a n i n g s   f r o m   C :   i o . s t d i n ,   i o . s t d o u t ,   a n d   
 
 i o . s t d e r r .   T h e   I / O   l i b r a r y   n e v e r   c l o s e s   t h e s e   f i l e s .   
 
 
 
 U n l e s s   o t h e r w i s e   s t a t e d ,   a l l   I / O   f u n c t i o n s   r e t u r n   n i l   o n   f a i l u r e   ( p l u s   a n   e r r o r   m e s s a g e   a s   a   s e c o n d   r e s u l t   a n d   a   
 
 s y s t e m - d e p e n d e n t   e r r o r   c o d e   a s   a   t h i r d   r e s u l t )   a n d   s o m e   v a l u e   d i f f e r e n t   f r o m   n i l   o n   s u c c e s s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . c l o s e   ( [ f i l e ] ) 
 
 E q u i v a l e n t   t o   f i l e : c l o s e ( ) .   W i t h o u t   a   f i l e ,   c l o s e s   t h e   d e f a u l t   o u t p u t   f i l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . f l u s h   ( ) 
 
 E q u i v a l e n t   t o   f i l e : f l u s h   o v e r   t h e   d e f a u l t   o u t p u t   f i l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . i n p u t   ( [ f i l e ] ) 
 
 W h e n   c a l l e d   w i t h   a   f i l e   n a m e ,   i t   o p e n s   t h e   n a m e d   f i l e   ( i n   t e x t   m o d e ) ,   a n d   s e t s   i t s   h a n d l e   a s   t h e   d e f a u l t   i n p u t   f i l e .   W h e n   
 
 c a l l e d   w i t h   a   f i l e   h a n d l e ,   i t   s i m p l y   s e t s   t h i s   f i l e   h a n d l e   a s   t h e   d e f a u l t   i n p u t   f i l e .   W h e n   c a l l e d   w i t h o u t   p a r a m e t e r s ,   i t   
 
 r e t u r n s   t h e   c u r r e n t   d e f a u l t   i n p u t   f i l e .   
 
 
 
 I n   c a s e   o f   e r r o r s   t h i s   f u n c t i o n   r a i s e s   t h e   e r r o r ,   i n s t e a d   o f   r e t u r n i n g   a n   e r r o r   c o d e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . l i n e s   ( [ f i l e n a m e ] ) 
 
 O p e n s   t h e   g i v e n   f i l e   n a m e   i n   r e a d   m o d e   a n d   r e t u r n s   a n   i t e r a t o r   f u n c t i o n   t h a t ,   e a c h   t i m e   i t   i s   c a l l e d ,   r e t u r n s   a   n e w   l i n e   
 
 f r o m   t h e   f i l e .   T h e r e f o r e ,   t h e   c o n s t r u c t i o n   
 
 
 
           f o r   l i n e   i n   i o . l i n e s ( f i l e n a m e )   d o   b o d y   e n d 
 
 
 
 w i l l   i t e r a t e   o v e r   a l l   l i n e s   o f   t h e   f i l e .   W h e n   t h e   i t e r a t o r   f u n c t i o n   d e t e c t s   t h e   e n d   o f   f i l e ,   i t   r e t u r n s   n i l   ( t o   f i n i s h   t h e   
 
 l o o p )   a n d   a u t o m a t i c a l l y   c l o s e s   t h e   f i l e .   
 
 
 
 T h e   c a l l   i o . l i n e s ( )   ( w i t h   n o   f i l e   n a m e )   i s   e q u i v a l e n t   t o   i o . i n p u t ( ) : l i n e s ( ) ;   t h a t   i s ,   i t   i t e r a t e s   o v e r   t h e   l i n e s   o f   t h e   
 
 d e f a u l t   i n p u t   f i l e .   I n   t h i s   c a s e   i t   d o e s   n o t   c l o s e   t h e   f i l e   w h e n   t h e   l o o p   e n d s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . o p e n   ( f i l e n a m e   [ ,   m o d e ] ) 
 
 T h i s   f u n c t i o n   o p e n s   a   f i l e ,   i n   t h e   m o d e   s p e c i f i e d   i n   t h e   s t r i n g   m o d e .   I t   r e t u r n s   a   n e w   f i l e   h a n d l e ,   o r ,   i n   c a s e   o f   e r r o r s ,   
 
 n i l   p l u s   a n   e r r o r   m e s s a g e .   
 
 
 
 T h e   m o d e   s t r i n g   c a n   b e   a n y   o f   t h e   f o l l o w i n g :   
 
 
 
 " r " :   r e a d   m o d e   ( t h e   d e f a u l t ) ;   
 
 " w " :   w r i t e   m o d e ;   
 
 " a " :   a p p e n d   m o d e ;   
 
 " r + " :   u p d a t e   m o d e ,   a l l   p r e v i o u s   d a t a   i s   p r e s e r v e d ;   
 
 " w + " :   u p d a t e   m o d e ,   a l l   p r e v i o u s   d a t a   i s   e r a s e d ;   
 
 " a + " :   a p p e n d   u p d a t e   m o d e ,   p r e v i o u s   d a t a   i s   p r e s e r v e d ,   w r i t i n g   i s   o n l y   a l l o w e d   a t   t h e   e n d   o f   f i l e .   
 
 T h e   m o d e   s t r i n g   c a n   a l s o   h a v e   a   ' b '   a t   t h e   e n d ,   w h i c h   i s   n e e d e d   i n   s o m e   s y s t e m s   t o   o p e n   t h e   f i l e   i n   b i n a r y   m o d e .   T h i s   
 
 s t r i n g   i s   e x a c t l y   w h a t   i s   u s e d   i n   t h e   s t a n d a r d   C   f u n c t i o n   f o p e n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . o u t p u t   ( [ f i l e ] ) 
 
 S i m i l a r   t o   i o . i n p u t ,   b u t   o p e r a t e s   o v e r   t h e   d e f a u l t   o u t p u t   f i l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . p o p e n   ( p r o g   [ ,   m o d e ] ) 
 
 S t a r t s   p r o g r a m   p r o g   i n   a   s e p a r a t e d   p r o c e s s   a n d   r e t u r n s   a   f i l e   h a n d l e   t h a t   y o u   c a n   u s e   t o   r e a d   d a t a   f r o m   t h i s   p r o g r a m   ( i f   
 
 m o d e   i s   " r " ,   t h e   d e f a u l t )   o r   t o   w r i t e   d a t a   t o   t h i s   p r o g r a m   ( i f   m o d e   i s   " w " ) .   
 
 
 
 T h i s   f u n c t i o n   i s   s y s t e m   d e p e n d e n t   a n d   i s   n o t   a v a i l a b l e   o n   a l l   p l a t f o r m s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . r e a d   (    ) 
 
 E q u i v a l e n t   t o   i o . i n p u t ( ) : r e a d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . t m p f i l e   ( ) 
 
 R e t u r n s   a   h a n d l e   f o r   a   t e m p o r a r y   f i l e .   T h i s   f i l e   i s   o p e n e d   i n   u p d a t e   m o d e   a n d   i t   i s   a u t o m a t i c a l l y   r e m o v e d   w h e n   t h e   p r o g r a m   
 
 e n d s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . t y p e   ( o b j ) 
 
 C h e c k s   w h e t h e r   o b j   i s   a   v a l i d   f i l e   h a n d l e .   R e t u r n s   t h e   s t r i n g   " f i l e "   i f   o b j   i s   a n   o p e n   f i l e   h a n d l e ,   " c l o s e d   f i l e "   i f   o b j   i s   
 
 a   c l o s e d   f i l e   h a n d l e ,   o r   n i l   i f   o b j   i s   n o t   a   f i l e   h a n d l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 i o . w r i t e   (    ) 
 
 E q u i v a l e n t   t o   i o . o u t p u t ( ) : w r i t e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : c l o s e   ( ) 
 
 C l o s e s   f i l e .   N o t e   t h a t   f i l e s   a r e   a u t o m a t i c a l l y   c l o s e d   w h e n   t h e i r   h a n d l e s   a r e   g a r b a g e   c o l l e c t e d ,   b u t   t h a t   t a k e s   a n   
 
 u n p r e d i c t a b l e   a m o u n t   o f   t i m e   t o   h a p p e n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : f l u s h   ( ) 
 
 S a v e s   a n y   w r i t t e n   d a t a   t o   f i l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : l i n e s   ( ) 
 
 R e t u r n s   a n   i t e r a t o r   f u n c t i o n   t h a t ,   e a c h   t i m e   i t   i s   c a l l e d ,   r e t u r n s   a   n e w   l i n e   f r o m   t h e   f i l e .   T h e r e f o r e ,   t h e   
 
 c o n s t r u c t i o n   
 
 
 
           f o r   l i n e   i n   f i l e : l i n e s ( )   d o   b o d y   e n d 
 
 
 
 w i l l   i t e r a t e   o v e r   a l l   l i n e s   o f   t h e   f i l e .   ( U n l i k e   i o . l i n e s ,   t h i s   f u n c t i o n   d o e s   n o t   c l o s e   t h e   f i l e   w h e n   t h e   l o o p   e n d s . )   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : r e a d   (    ) 
 
 R e a d s   t h e   f i l e   f i l e ,   a c c o r d i n g   t o   t h e   g i v e n   f o r m a t s ,   w h i c h   s p e c i f y   w h a t   t o   r e a d .   F o r   e a c h   f o r m a t ,   t h e   f u n c t i o n   r e t u r n s   a   
 
 s t r i n g   ( o r   a   n u m b e r )   w i t h   t h e   c h a r a c t e r s   r e a d ,   o r   n i l   i f   i t   c a n n o t   r e a d   d a t a   w i t h   t h e   s p e c i f i e d   f o r m a t .   W h e n   c a l l e d   w i t h o u t   
 
 f o r m a t s ,   i t   u s e s   a   d e f a u l t   f o r m a t   t h a t   r e a d s   t h e   e n t i r e   n e x t   l i n e   ( s e e   b e l o w ) .   
 
 
 
 T h e   a v a i l a b l e   f o r m a t s   a r e   
 
 
 
 " * n " :   r e a d s   a   n u m b e r ;   t h i s   i s   t h e   o n l y   f o r m a t   t h a t   r e t u r n s   a   n u m b e r   i n s t e a d   o f   a   s t r i n g .   
 
 " * a " :   r e a d s   t h e   w h o l e   f i l e ,   s t a r t i n g   a t   t h e   c u r r e n t   p o s i t i o n .   O n   e n d   o f   f i l e ,   i t   r e t u r n s   t h e   e m p t y   s t r i n g .   
 
 " * l " :   r e a d s   t h e   n e x t   l i n e   ( s k i p p i n g   t h e   e n d   o f   l i n e ) ,   r e t u r n i n g   n i l   o n   e n d   o f   f i l e .   T h i s   i s   t h e   d e f a u l t   f o r m a t .   
 
 n u m b e r :   r e a d s   a   s t r i n g   w i t h   u p   t o   t h i s   n u m b e r   o f   c h a r a c t e r s ,   r e t u r n i n g   n i l   o n   e n d   o f   f i l e .   I f   n u m b e r   i s   z e r o ,   i t   r e a d s   
 
 n o t h i n g   a n d   r e t u r n s   a n   e m p t y   s t r i n g ,   o r   n i l   o n   e n d   o f   f i l e .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : s e e k   ( [ w h e n c e ]   [ ,   o f f s e t ] ) 
 
 S e t s   a n d   g e t s   t h e   f i l e   p o s i t i o n ,   m e a s u r e d   f r o m   t h e   b e g i n n i n g   o f   t h e   f i l e ,   t o   t h e   p o s i t i o n   g i v e n   b y   o f f s e t   p l u s   a   b a s e   
 
 s p e c i f i e d   b y   t h e   s t r i n g   w h e n c e ,   a s   f o l l o w s :   
 
 
 
 " s e t " :   b a s e   i s   p o s i t i o n   0   ( b e g i n n i n g   o f   t h e   f i l e ) ;   
 
 " c u r " :   b a s e   i s   c u r r e n t   p o s i t i o n ;   
 
 " e n d " :   b a s e   i s   e n d   o f   f i l e ;   
 
 I n   c a s e   o f   s u c c e s s ,   f u n c t i o n   s e e k   r e t u r n s   t h e   f i n a l   f i l e   p o s i t i o n ,   m e a s u r e d   i n   b y t e s   f r o m   t h e   b e g i n n i n g   o f   t h e   f i l e .   I f   
 
 t h i s   f u n c t i o n   f a i l s ,   i t   r e t u r n s   n i l ,   p l u s   a   s t r i n g   d e s c r i b i n g   t h e   e r r o r .   
 
 
 
 T h e   d e f a u l t   v a l u e   f o r   w h e n c e   i s   " c u r " ,   a n d   f o r   o f f s e t   i s   0 .   T h e r e f o r e ,   t h e   c a l l   f i l e : s e e k ( )   r e t u r n s   t h e   c u r r e n t   f i l e   
 
 p o s i t i o n ,   w i t h o u t   c h a n g i n g   i t ;   t h e   c a l l   f i l e : s e e k ( " s e t " )   s e t s   t h e   p o s i t i o n   t o   t h e   b e g i n n i n g   o f   t h e   f i l e   ( a n d   r e t u r n s   0 ) ;   
 
 a n d   t h e   c a l l   f i l e : s e e k ( " e n d " )   s e t s   t h e   p o s i t i o n   t o   t h e   e n d   o f   t h e   f i l e ,   a n d   r e t u r n s   i t s   s i z e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : s e t v b u f   ( m o d e   [ ,   s i z e ] ) 
 
 S e t s   t h e   b u f f e r i n g   m o d e   f o r   a n   o u t p u t   f i l e .   T h e r e   a r e   t h r e e   a v a i l a b l e   m o d e s :   
 
 
 
 " n o " :   n o   b u f f e r i n g ;   t h e   r e s u l t   o f   a n y   o u t p u t   o p e r a t i o n   a p p e a r s   i m m e d i a t e l y .   
 
 " f u l l " :   f u l l   b u f f e r i n g ;   o u t p u t   o p e r a t i o n   i s   p e r f o r m e d   o n l y   w h e n   t h e   b u f f e r   i s   f u l l   ( o r   w h e n   y o u   e x p l i c i t l y   f l u s h   t h e   f i l e   
 
 ( s e e   i o . f l u s h ) ) .   
 
 " l i n e " :   l i n e   b u f f e r i n g ;   o u t p u t   i s   b u f f e r e d   u n t i l   a   n e w l i n e   i s   o u t p u t   o r   t h e r e   i s   a n y   i n p u t   f r o m   s o m e   s p e c i a l   f i l e s   ( s u c h   a s   
 
 a   t e r m i n a l   d e v i c e ) .   
 
 F o r   t h e   l a s t   t w o   c a s e s ,   s i z e   s p e c i f i e s   t h e   s i z e   o f   t h e   b u f f e r ,   i n   b y t e s .   T h e   d e f a u l t   i s   a n   a p p r o p r i a t e   s i z e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f i l e : w r i t e   (    ) 
 
 W r i t e s   t h e   v a l u e   o f   e a c h   o f   i t s   a r g u m e n t s   t o   t h e   f i l e .   T h e   a r g u m e n t s   m u s t   b e   s t r i n g s   o r   n u m b e r s .   T o   w r i t e   o t h e r   v a l u e s ,   u s e   
 
 t o s t r i n g   o r   s t r i n g . f o r m a t   b e f o r e   w r i t e .   
 
 
 
 5 . 8   -   O p e r a t i n g   S y s t e m   F a c i l i t i e s 
 
 T h i s   l i b r a r y   i s   i m p l e m e n t e d   t h r o u g h   t a b l e   o s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . c l o c k   ( ) 
 
 R e t u r n s   a n   a p p r o x i m a t i o n   o f   t h e   a m o u n t   i n   s e c o n d s   o f   C P U   t i m e   u s e d   b y   t h e   p r o g r a m .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . d a t e   ( [ f o r m a t   [ ,   t i m e ] ] ) 
 
 R e t u r n s   a   s t r i n g   o r   a   t a b l e   c o n t a i n i n g   d a t e   a n d   t i m e ,   f o r m a t t e d   a c c o r d i n g   t o   t h e   g i v e n   s t r i n g   f o r m a t .   
 
 
 
 I f   t h e   t i m e   a r g u m e n t   i s   p r e s e n t ,   t h i s   i s   t h e   t i m e   t o   b e   f o r m a t t e d   ( s e e   t h e   o s . t i m e   f u n c t i o n   f o r   a   d e s c r i p t i o n   o f   t h i s   
 
 v a l u e ) .   O t h e r w i s e ,   d a t e   f o r m a t s   t h e   c u r r e n t   t i m e .   
 
 
 
 I f   f o r m a t   s t a r t s   w i t h   ' ! ' ,   t h e n   t h e   d a t e   i s   f o r m a t t e d   i n   C o o r d i n a t e d   U n i v e r s a l   T i m e .   A f t e r   t h i s   o p t i o n a l   c h a r a c t e r ,   i f   
 
 f o r m a t   i s   t h e   s t r i n g   " * t " ,   t h e n   d a t e   r e t u r n s   a   t a b l e   w i t h   t h e   f o l l o w i n g   f i e l d s :   y e a r   ( f o u r   d i g i t s ) ,   m o n t h   ( 1 - - 1 2 ) ,   d a y   
 
 ( 1 - - 3 1 ) ,   h o u r   ( 0 - - 2 3 ) ,   m i n   ( 0 - - 5 9 ) ,   s e c   ( 0 - - 6 1 ) ,   w d a y   ( w e e k d a y ,   S u n d a y   i s   1 ) ,   y d a y   ( d a y   o f   t h e   y e a r ) ,   a n d   i s d s t   ( d a y l i g h t   
 
 s a v i n g   f l a g ,   a   b o o l e a n ) .   
 
 
 
 I f   f o r m a t   i s   n o t   " * t " ,   t h e n   d a t e   r e t u r n s   t h e   d a t e   a s   a   s t r i n g ,   f o r m a t t e d   a c c o r d i n g   t o   t h e   s a m e   r u l e s   a s   t h e   C   f u n c t i o n   
 
 s t r f t i m e .   
 
 
 
 W h e n   c a l l e d   w i t h o u t   a r g u m e n t s ,   d a t e   r e t u r n s   a   r e a s o n a b l e   d a t e   a n d   t i m e   r e p r e s e n t a t i o n   t h a t   d e p e n d s   o n   t h e   h o s t   s y s t e m   a n d   
 
 o n   t h e   c u r r e n t   l o c a l e   ( t h a t   i s ,   o s . d a t e ( )   i s   e q u i v a l e n t   t o   o s . d a t e ( " % c " ) ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . d i f f t i m e   ( t 2 ,   t 1 ) 
 
 R e t u r n s   t h e   n u m b e r   o f   s e c o n d s   f r o m   t i m e   t 1   t o   t i m e   t 2 .   I n   P O S I X ,   W i n d o w s ,   a n d   s o m e   o t h e r   s y s t e m s ,   t h i s   v a l u e   i s   e x a c t l y   
 
 t 2 - t 1 .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . e x e c u t e   ( [ c o m m a n d ] ) 
 
 T h i s   f u n c t i o n   i s   e q u i v a l e n t   t o   t h e   C   f u n c t i o n   s y s t e m .   I t   p a s s e s   c o m m a n d   t o   b e   e x e c u t e d   b y   a n   o p e r a t i n g   s y s t e m   s h e l l .   I t   
 
 r e t u r n s   a   s t a t u s   c o d e ,   w h i c h   i s   s y s t e m - d e p e n d e n t .   I f   c o m m a n d   i s   a b s e n t ,   t h e n   i t   r e t u r n s   n o n z e r o   i f   a   s h e l l   i s   a v a i l a b l e   a n d   
 
 z e r o   o t h e r w i s e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . e x i t   ( [ c o d e ] ) 
 
 C a l l s   t h e   C   f u n c t i o n   e x i t ,   w i t h   a n   o p t i o n a l   c o d e ,   t o   t e r m i n a t e   t h e   h o s t   p r o g r a m .   T h e   d e f a u l t   v a l u e   f o r   c o d e   i s   t h e   s u c c e s s   
 
 c o d e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . g e t e n v   ( v a r n a m e ) 
 
 R e t u r n s   t h e   v a l u e   o f   t h e   p r o c e s s   e n v i r o n m e n t   v a r i a b l e   v a r n a m e ,   o r   n i l   i f   t h e   v a r i a b l e   i s   n o t   d e f i n e d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . r e m o v e   ( f i l e n a m e ) 
 
 D e l e t e s   t h e   f i l e   o r   d i r e c t o r y   w i t h   t h e   g i v e n   n a m e .   D i r e c t o r i e s   m u s t   b e   e m p t y   t o   b e   r e m o v e d .   I f   t h i s   f u n c t i o n   f a i l s ,   i t   
 
 r e t u r n s   n i l ,   p l u s   a   s t r i n g   d e s c r i b i n g   t h e   e r r o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . r e n a m e   ( o l d n a m e ,   n e w n a m e ) 
 
 R e n a m e s   f i l e   o r   d i r e c t o r y   n a m e d   o l d n a m e   t o   n e w n a m e .   I f   t h i s   f u n c t i o n   f a i l s ,   i t   r e t u r n s   n i l ,   p l u s   a   s t r i n g   d e s c r i b i n g   t h e   
 
 e r r o r .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . s e t l o c a l e   ( l o c a l e   [ ,   c a t e g o r y ] ) 
 
 S e t s   t h e   c u r r e n t   l o c a l e   o f   t h e   p r o g r a m .   l o c a l e   i s   a   s t r i n g   s p e c i f y i n g   a   l o c a l e ;   c a t e g o r y   i s   a n   o p t i o n a l   s t r i n g   d e s c r i b i n g   
 
 w h i c h   c a t e g o r y   t o   c h a n g e :   " a l l " ,   " c o l l a t e " ,   " c t y p e " ,   " m o n e t a r y " ,   " n u m e r i c " ,   o r   " t i m e " ;   t h e   d e f a u l t   c a t e g o r y   i s   " a l l " .   T h e   
 
 f u n c t i o n   r e t u r n s   t h e   n a m e   o f   t h e   n e w   l o c a l e ,   o r   n i l   i f   t h e   r e q u e s t   c a n n o t   b e   h o n o r e d .   
 
 
 
 I f   l o c a l e   i s   t h e   e m p t y   s t r i n g ,   t h e   c u r r e n t   l o c a l e   i s   s e t   t o   a n   i m p l e m e n t a t i o n - d e f i n e d   n a t i v e   l o c a l e .   I f   l o c a l e   i s   t h e   
 
 s t r i n g   " C " ,   t h e   c u r r e n t   l o c a l e   i s   s e t   t o   t h e   s t a n d a r d   C   l o c a l e .   
 
 
 
 W h e n   c a l l e d   w i t h   n i l   a s   t h e   f i r s t   a r g u m e n t ,   t h i s   f u n c t i o n   o n l y   r e t u r n s   t h e   n a m e   o f   t h e   c u r r e n t   l o c a l e   f o r   t h e   g i v e n   
 
 c a t e g o r y .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . t i m e   ( [ t a b l e ] ) 
 
 R e t u r n s   t h e   c u r r e n t   t i m e   w h e n   c a l l e d   w i t h o u t   a r g u m e n t s ,   o r   a   t i m e   r e p r e s e n t i n g   t h e   d a t e   a n d   t i m e   s p e c i f i e d   b y   t h e   g i v e n   
 
 t a b l e .   T h i s   t a b l e   m u s t   h a v e   f i e l d s   y e a r ,   m o n t h ,   a n d   d a y ,   a n d   m a y   h a v e   f i e l d s   h o u r ,   m i n ,   s e c ,   a n d   i s d s t   ( f o r   a   d e s c r i p t i o n   
 
 o f   t h e s e   f i e l d s ,   s e e   t h e   o s . d a t e   f u n c t i o n ) .   
 
 
 
 T h e   r e t u r n e d   v a l u e   i s   a   n u m b e r ,   w h o s e   m e a n i n g   d e p e n d s   o n   y o u r   s y s t e m .   I n   P O S I X ,   W i n d o w s ,   a n d   s o m e   o t h e r   s y s t e m s ,   t h i s   
 
 n u m b e r   c o u n t s   t h e   n u m b e r   o f   s e c o n d s   s i n c e   s o m e   g i v e n   s t a r t   t i m e   ( t h e   " e p o c h " ) .   I n   o t h e r   s y s t e m s ,   t h e   m e a n i n g   i s   n o t   
 
 s p e c i f i e d ,   a n d   t h e   n u m b e r   r e t u r n e d   b y   t i m e   c a n   b e   u s e d   o n l y   a s   a n   a r g u m e n t   t o   d a t e   a n d   d i f f t i m e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 o s . t m p n a m e   ( ) 
 
 R e t u r n s   a   s t r i n g   w i t h   a   f i l e   n a m e   t h a t   c a n   b e   u s e d   f o r   a   t e m p o r a r y   f i l e .   T h e   f i l e   m u s t   b e   e x p l i c i t l y   o p e n e d   b e f o r e   i t s   u s e   
 
 a n d   e x p l i c i t l y   r e m o v e d   w h e n   n o   l o n g e r   n e e d e d .   
 
 
 
 O n   s o m e   s y s t e m s   ( P O S I X ) ,   t h i s   f u n c t i o n   a l s o   c r e a t e s   a   f i l e   w i t h   t h a t   n a m e ,   t o   a v o i d   s e c u r i t y   r i s k s .   ( S o m e o n e   e l s e   m i g h t   
 
 c r e a t e   t h e   f i l e   w i t h   w r o n g   p e r m i s s i o n s   i n   t h e   t i m e   b e t w e e n   g e t t i n g   t h e   n a m e   a n d   c r e a t i n g   t h e   f i l e . )   Y o u   s t i l l   h a v e   t o   o p e n   
 
 t h e   f i l e   t o   u s e   i t   a n d   t o   r e m o v e   i t   ( e v e n   i f   y o u   d o   n o t   u s e   i t ) .   
 
 
 
 W h e n   p o s s i b l e ,   y o u   m a y   p r e f e r   t o   u s e   i o . t m p f i l e ,   w h i c h   a u t o m a t i c a l l y   r e m o v e s   t h e   f i l e   w h e n   t h e   p r o g r a m   e n d s .   
 
 
 
 5 . 9   -   T h e   D e b u g   L i b r a r y 
 
 T h i s   l i b r a r y   p r o v i d e s   t h e   f u n c t i o n a l i t y   o f   t h e   d e b u g   i n t e r f a c e   t o   L u a   p r o g r a m s .   Y o u   s h o u l d   e x e r t   c a r e   w h e n   u s i n g   t h i s   
 
 l i b r a r y .   T h e   f u n c t i o n s   p r o v i d e d   h e r e   s h o u l d   b e   u s e d   e x c l u s i v e l y   f o r   d e b u g g i n g   a n d   s i m i l a r   t a s k s ,   s u c h   a s   p r o f i l i n g .   P l e a s e   
 
 r e s i s t   t h e   t e m p t a t i o n   t o   u s e   t h e m   a s   a   u s u a l   p r o g r a m m i n g   t o o l :   t h e y   c a n   b e   v e r y   s l o w .   M o r e o v e r ,   s e v e r a l   o f   t h e s e   f u n c t i o n s   
 
 v i o l a t e   s o m e   a s s u m p t i o n s   a b o u t   L u a   c o d e   ( e . g . ,   t h a t   v a r i a b l e s   l o c a l   t o   a   f u n c t i o n   c a n n o t   b e   a c c e s s e d   f r o m   o u t s i d e   o r   t h a t   
 
 u s e r d a t a   m e t a t a b l e s   c a n n o t   b e   c h a n g e d   b y   L u a   c o d e )   a n d   t h e r e f o r e   c a n   c o m p r o m i s e   o t h e r w i s e   s e c u r e   c o d e .   
 
 
 
 A l l   f u n c t i o n s   i n   t h i s   l i b r a r y   a r e   p r o v i d e d   i n s i d e   t h e   d e b u g   t a b l e .   A l l   f u n c t i o n s   t h a t   o p e r a t e   o v e r   a   t h r e a d   h a v e   a n   
 
 o p t i o n a l   f i r s t   a r g u m e n t   w h i c h   i s   t h e   t h r e a d   t o   o p e r a t e   o v e r .   T h e   d e f a u l t   i s   a l w a y s   t h e   c u r r e n t   t h r e a d .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . d e b u g   ( ) 
 
 E n t e r s   a n   i n t e r a c t i v e   m o d e   w i t h   t h e   u s e r ,   r u n n i n g   e a c h   s t r i n g   t h a t   t h e   u s e r   e n t e r s .   U s i n g   s i m p l e   c o m m a n d s   a n d   o t h e r   d e b u g   
 
 f a c i l i t i e s ,   t h e   u s e r   c a n   i n s p e c t   g l o b a l   a n d   l o c a l   v a r i a b l e s ,   c h a n g e   t h e i r   v a l u e s ,   e v a l u a t e   e x p r e s s i o n s ,   a n d   s o   o n .   A   l i n e   
 
 c o n t a i n i n g   o n l y   t h e   w o r d   c o n t   f i n i s h e s   t h i s   f u n c t i o n ,   s o   t h a t   t h e   c a l l e r   c o n t i n u e s   i t s   e x e c u t i o n .   
 
 
 
 N o t e   t h a t   c o m m a n d s   f o r   d e b u g . d e b u g   a r e   n o t   l e x i c a l l y   n e s t e d   w i t h i n   a n y   f u n c t i o n ,   a n d   s o   h a v e   n o   d i r e c t   a c c e s s   t o   l o c a l   
 
 v a r i a b l e s .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t f e n v   ( o ) 
 
 R e t u r n s   t h e   e n v i r o n m e n t   o f   o b j e c t   o .   
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t h o o k   ( [ t h r e a d ] ) 
 
 R e t u r n s   t h e   c u r r e n t   h o o k   s e t t i n g s   o f   t h e   t h r e a d ,   a s   t h r e e   v a l u e s :   t h e   c u r r e n t   h o o k   f u n c t i o n ,   t h e   c u r r e n t   h o o k   m a s k ,   a n d   t h e   
 
 c u r r e n t   h o o k   c o u n t   ( a s   s e t   b y   t h e   d e b u g . s e t h o o k   f u n c t i o n ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t i n f o   ( [ t h r e a d , ]   f u n c t i o n   [ ,   w h a t ] ) 
 
 R e t u r n s   a   t a b l e   w i t h   i n f o r m a t i o n   a b o u t   a   f u n c t i o n .   Y o u   c a n   g i v e   t h e   f u n c t i o n   d i r e c t l y ,   o r   y o u   c a n   g i v e   a   n u m b e r   a s   t h e   
 
 v a l u e   o f   f u n c t i o n ,   w h i c h   m e a n s   t h e   f u n c t i o n   r u n n i n g   a t   l e v e l   f u n c t i o n   o f   t h e   c a l l   s t a c k   o f   t h e   g i v e n   t h r e a d :   l e v e l   0   i s   t h e   
 
 c u r r e n t   f u n c t i o n   ( g e t i n f o   i t s e l f ) ;   l e v e l   1   i s   t h e   f u n c t i o n   t h a t   c a l l e d   g e t i n f o ;   a n d   s o   o n .   I f   f u n c t i o n   i s   a   n u m b e r   l a r g e r   
 
 t h a n   t h e   n u m b e r   o f   a c t i v e   f u n c t i o n s ,   t h e n   g e t i n f o   r e t u r n s   n i l .   
 
 
 
 T h e   r e t u r n e d   t a b l e   c a n   c o n t a i n   a l l   t h e   f i e l d s   r e t u r n e d   b y   l u a _ g e t i n f o ,   w i t h   t h e   s t r i n g   w h a t   d e s c r i b i n g   w h i c h   f i e l d s   t o   f i l l   
 
 i n .   T h e   d e f a u l t   f o r   w h a t   i s   t o   g e t   a l l   i n f o r m a t i o n   a v a i l a b l e ,   e x c e p t   t h e   t a b l e   o f   v a l i d   l i n e s .   I f   p r e s e n t ,   t h e   o p t i o n   ' f '   
 
 a d d s   a   f i e l d   n a m e d   f u n c   w i t h   t h e   f u n c t i o n   i t s e l f .   I f   p r e s e n t ,   t h e   o p t i o n   ' L '   a d d s   a   f i e l d   n a m e d   a c t i v e l i n e s   w i t h   t h e   t a b l e   
 
 o f   v a l i d   l i n e s .   
 
 
 
 F o r   i n s t a n c e ,   t h e   e x p r e s s i o n   d e b u g . g e t i n f o ( 1 , " n " ) . n a m e   r e t u r n s   a   t a b l e   w i t h   a   n a m e   f o r   t h e   c u r r e n t   f u n c t i o n ,   i f   a   
 
 r e a s o n a b l e   n a m e   c a n   b e   f o u n d ,   a n d   t h e   e x p r e s s i o n   d e b u g . g e t i n f o ( p r i n t )   r e t u r n s   a   t a b l e   w i t h   a l l   a v a i l a b l e   i n f o r m a t i o n   a b o u t   
 
 t h e   p r i n t   f u n c t i o n .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t l o c a l   ( [ t h r e a d , ]   l e v e l ,   l o c a l ) 
 
 T h i s   f u n c t i o n   r e t u r n s   t h e   n a m e   a n d   t h e   v a l u e   o f   t h e   l o c a l   v a r i a b l e   w i t h   i n d e x   l o c a l   o f   t h e   f u n c t i o n   a t   l e v e l   l e v e l   o f   t h e   
 
 s t a c k .   ( T h e   f i r s t   p a r a m e t e r   o r   l o c a l   v a r i a b l e   h a s   i n d e x   1 ,   a n d   s o   o n ,   u n t i l   t h e   l a s t   a c t i v e   l o c a l   v a r i a b l e . )   T h e   f u n c t i o n   
 
 r e t u r n s   n i l   i f   t h e r e   i s   n o   l o c a l   v a r i a b l e   w i t h   t h e   g i v e n   i n d e x ,   a n d   r a i s e s   a n   e r r o r   w h e n   c a l l e d   w i t h   a   l e v e l   o u t   o f   r a n g e .   
 
 ( Y o u   c a n   c a l l   d e b u g . g e t i n f o   t o   c h e c k   w h e t h e r   t h e   l e v e l   i s   v a l i d . )   
 
 
 
 V a r i a b l e   n a m e s   s t a r t i n g   w i t h   ' ( '   ( o p e n   p a r e n t h e s e s )   r e p r e s e n t   i n t e r n a l   v a r i a b l e s   ( l o o p   c o n t r o l   v a r i a b l e s ,   t e m p o r a r i e s ,   a n d   
 
 C   f u n c t i o n   l o c a l s ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t m e t a t a b l e   ( o b j e c t ) 
 
 R e t u r n s   t h e   m e t a t a b l e   o f   t h e   g i v e n   o b j e c t   o r   n i l   i f   i t   d o e s   n o t   h a v e   a   m e t a t a b l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t r e g i s t r y   ( ) 
 
 R e t u r n s   t h e   r e g i s t r y   t a b l e   ( s e e    3 . 5 ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . g e t u p v a l u e   ( f u n c ,   u p ) 
 
 T h i s   f u n c t i o n   r e t u r n s   t h e   n a m e   a n d   t h e   v a l u e   o f   t h e   u p v a l u e   w i t h   i n d e x   u p   o f   t h e   f u n c t i o n   f u n c .   T h e   f u n c t i o n   r e t u r n s   n i l   
 
 i f   t h e r e   i s   n o   u p v a l u e   w i t h   t h e   g i v e n   i n d e x .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . s e t f e n v   ( o b j e c t ,   t a b l e ) 
 
 S e t s   t h e   e n v i r o n m e n t   o f   t h e   g i v e n   o b j e c t   t o   t h e   g i v e n   t a b l e .   R e t u r n s   o b j e c t .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . s e t h o o k   ( [ t h r e a d , ]   h o o k ,   m a s k   [ ,   c o u n t ] ) 
 
 S e t s   t h e   g i v e n   f u n c t i o n   a s   a   h o o k .   T h e   s t r i n g   m a s k   a n d   t h e   n u m b e r   c o u n t   d e s c r i b e   w h e n   t h e   h o o k   w i l l   b e   c a l l e d .   T h e   s t r i n g   
 
 m a s k   m a y   h a v e   t h e   f o l l o w i n g   c h a r a c t e r s ,   w i t h   t h e   g i v e n   m e a n i n g :   
 
 
 
 " c " :   t h e   h o o k   i s   c a l l e d   e v e r y   t i m e   L u a   c a l l s   a   f u n c t i o n ;   
 
 " r " :   t h e   h o o k   i s   c a l l e d   e v e r y   t i m e   L u a   r e t u r n s   f r o m   a   f u n c t i o n ;   
 
 " l " :   t h e   h o o k   i s   c a l l e d   e v e r y   t i m e   L u a   e n t e r s   a   n e w   l i n e   o f   c o d e .   
 
 W i t h   a   c o u n t   d i f f e r e n t   f r o m   z e r o ,   t h e   h o o k   i s   c a l l e d   a f t e r   e v e r y   c o u n t   i n s t r u c t i o n s .   
 
 
 
 W h e n   c a l l e d   w i t h o u t   a r g u m e n t s ,   d e b u g . s e t h o o k   t u r n s   o f f   t h e   h o o k .   
 
 
 
 W h e n   t h e   h o o k   i s   c a l l e d ,   i t s   f i r s t   p a r a m e t e r   i s   a   s t r i n g   d e s c r i b i n g   t h e   e v e n t   t h a t   h a s   t r i g g e r e d   i t s   c a l l :   " c a l l " ,   " r e t u r n "   
 
 ( o r   " t a i l   r e t u r n " ,   w h e n   s i m u l a t i n g   a   r e t u r n   f r o m   a   t a i l   c a l l ) ,   " l i n e " ,   a n d   " c o u n t " .   F o r   l i n e   e v e n t s ,   t h e   h o o k   a l s o   g e t s   t h e   
 
 n e w   l i n e   n u m b e r   a s   i t s   s e c o n d   p a r a m e t e r .   I n s i d e   a   h o o k ,   y o u   c a n   c a l l   g e t i n f o   w i t h   l e v e l   2   t o   g e t   m o r e   i n f o r m a t i o n   a b o u t   t h e   
 
 r u n n i n g   f u n c t i o n   ( l e v e l   0   i s   t h e   g e t i n f o   f u n c t i o n ,   a n d   l e v e l   1   i s   t h e   h o o k   f u n c t i o n ) ,   u n l e s s   t h e   e v e n t   i s   " t a i l   r e t u r n " .   I n   
 
 t h i s   c a s e ,   L u a   i s   o n l y   s i m u l a t i n g   t h e   r e t u r n ,   a n d   a   c a l l   t o   g e t i n f o   w i l l   r e t u r n   i n v a l i d   d a t a .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . s e t l o c a l   ( [ t h r e a d , ]   l e v e l ,   l o c a l ,   v a l u e ) 
 
 T h i s   f u n c t i o n   a s s i g n s   t h e   v a l u e   v a l u e   t o   t h e   l o c a l   v a r i a b l e   w i t h   i n d e x   l o c a l   o f   t h e   f u n c t i o n   a t   l e v e l   l e v e l   o f   t h e   s t a c k .   
 
 T h e   f u n c t i o n   r e t u r n s   n i l   i f   t h e r e   i s   n o   l o c a l   v a r i a b l e   w i t h   t h e   g i v e n   i n d e x ,   a n d   r a i s e s   a n   e r r o r   w h e n   c a l l e d   w i t h   a   l e v e l   
 
 o u t   o f   r a n g e .   ( Y o u   c a n   c a l l   g e t i n f o   t o   c h e c k   w h e t h e r   t h e   l e v e l   i s   v a l i d . )   O t h e r w i s e ,   i t   r e t u r n s   t h e   n a m e   o f   t h e   l o c a l   
 
 v a r i a b l e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . s e t m e t a t a b l e   ( o b j e c t ,   t a b l e ) 
 
 S e t s   t h e   m e t a t a b l e   f o r   t h e   g i v e n   o b j e c t   t o   t h e   g i v e n   t a b l e   ( w h i c h   c a n   b e   n i l ) .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . s e t u p v a l u e   ( f u n c ,   u p ,   v a l u e ) 
 
 T h i s   f u n c t i o n   a s s i g n s   t h e   v a l u e   v a l u e   t o   t h e   u p v a l u e   w i t h   i n d e x   u p   o f   t h e   f u n c t i o n   f u n c .   T h e   f u n c t i o n   r e t u r n s   n i l   i f   t h e r e   
 
 i s   n o   u p v a l u e   w i t h   t h e   g i v e n   i n d e x .   O t h e r w i s e ,   i t   r e t u r n s   t h e   n a m e   o f   t h e   u p v a l u e .   
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 d e b u g . t r a c e b a c k   ( [ t h r e a d , ]   [ m e s s a g e   [ ,   l e v e l ] ] ) 
 
 R e t u r n s   a   s t r i n g   w i t h   a   t r a c e b a c k   o f   t h e   c a l l   s t a c k .   A n   o p t i o n a l   m e s s a g e   s t r i n g   i s   a p p e n d e d   a t   t h e   b e g i n n i n g   o f   t h e   
 
 t r a c e b a c k .   A n   o p t i o n a l   l e v e l   n u m b e r   t e l l s   a t   w h i c h   l e v e l   t o   s t a r t   t h e   t r a c e b a c k   ( d e f a u l t   i s   1 ,   t h e   f u n c t i o n   c a l l i n g   
 
 t r a c e b a c k ) .   
 
 
 
 6   -   L u a   S t a n d - a l o n e 
 
 A l t h o u g h   L u a   h a s   b e e n   d e s i g n e d   a s   a n   e x t e n s i o n   l a n g u a g e ,   t o   b e   e m b e d d e d   i n   a   h o s t   C   p r o g r a m ,   i t   i s   a l s o   f r e q u e n t l y   u s e d   a s   
 
 a   s t a n d - a l o n e   l a n g u a g e .   A n   i n t e r p r e t e r   f o r   L u a   a s   a   s t a n d - a l o n e   l a n g u a g e ,   c a l l e d   s i m p l y   l u a ,   i s   p r o v i d e d   w i t h   t h e   s t a n d a r d   
 
 d i s t r i b u t i o n .   T h e   s t a n d - a l o n e   i n t e r p r e t e r   i n c l u d e s   a l l   s t a n d a r d   l i b r a r i e s ,   i n c l u d i n g   t h e   d e b u g   l i b r a r y .   I t s   u s a g e   i s :   
 
 
 
           l u a   [ o p t i o n s ]   [ s c r i p t   [ a r g s ] ] 
 
 
 
 T h e   o p t i o n s   a r e :   
 
 
 
 - e   s t a t :   e x e c u t e s   s t r i n g   s t a t ;   
 
 - l   m o d :   " r e q u i r e s "   m o d ;   
 
 - i :   e n t e r s   i n t e r a c t i v e   m o d e   a f t e r   r u n n i n g   s c r i p t ;   
 
 - v :   p r i n t s   v e r s i o n   i n f o r m a t i o n ;   
 
 - - :   s t o p s   h a n d l i n g   o p t i o n s ;   
 
 - :   e x e c u t e s   s t d i n   a s   a   f i l e   a n d   s t o p s   h a n d l i n g   o p t i o n s .   
 
 A f t e r   h a n d l i n g   i t s   o p t i o n s ,   l u a   r u n s   t h e   g i v e n   s c r i p t ,   p a s s i n g   t o   i t   t h e   g i v e n   a r g s   a s   s t r i n g   a r g u m e n t s .   W h e n   c a l l e d   
 
 w i t h o u t   a r g u m e n t s ,   l u a   b e h a v e s   a s   l u a   - v   - i   w h e n   t h e   s t a n d a r d   i n p u t   ( s t d i n )   i s   a   t e r m i n a l ,   a n d   a s   l u a   -   o t h e r w i s e .   
 
 
 
 B e f o r e   r u n n i n g   a n y   a r g u m e n t ,   t h e   i n t e r p r e t e r   c h e c k s   f o r   a n   e n v i r o n m e n t   v a r i a b l e   L U A _ I N I T .   I f   i t s   f o r m a t   i s   @ f i l e n a m e ,   t h e n   
 
 l u a   e x e c u t e s   t h e   f i l e .   O t h e r w i s e ,   l u a   e x e c u t e s   t h e   s t r i n g   i t s e l f .   
 
 
 
 A l l   o p t i o n s   a r e   h a n d l e d   i n   o r d e r ,   e x c e p t   - i .   F o r   i n s t a n c e ,   a n   i n v o c a t i o n   l i k e   
 
 
 
           $   l u a   - e ' a = 1 '   - e   ' p r i n t ( a ) '   s c r i p t . l u a 
 
 
 
 w i l l   f i r s t   s e t   a   t o   1 ,   t h e n   p r i n t   t h e   v a l u e   o f   a   ( w h i c h   i s   ' 1 ' ) ,   a n d   f i n a l l y   r u n   t h e   f i l e   s c r i p t . l u a   w i t h   n o   a r g u m e n t s .   
 
 ( H e r e   $   i s   t h e   s h e l l   p r o m p t .   Y o u r   p r o m p t   m a y   b e   d i f f e r e n t . )   
 
 
 
 B e f o r e   s t a r t i n g   t o   r u n   t h e   s c r i p t ,   l u a   c o l l e c t s   a l l   a r g u m e n t s   i n   t h e   c o m m a n d   l i n e   i n   a   g l o b a l   t a b l e   c a l l e d   a r g .   T h e   s c r i p t   
 
 n a m e   i s   s t o r e d   a t   i n d e x   0 ,   t h e   f i r s t   a r g u m e n t   a f t e r   t h e   s c r i p t   n a m e   g o e s   t o   i n d e x   1 ,   a n d   s o   o n .   A n y   a r g u m e n t s   b e f o r e   t h e   
 
 s c r i p t   n a m e   ( t h a t   i s ,   t h e   i n t e r p r e t e r   n a m e   p l u s   t h e   o p t i o n s )   g o   t o   n e g a t i v e   i n d i c e s .   F o r   i n s t a n c e ,   i n   t h e   c a l l   
 
 
 
           $   l u a   - l a   b . l u a   t 1   t 2 
 
 
 
 t h e   i n t e r p r e t e r   f i r s t   r u n s   t h e   f i l e   a . l u a ,   t h e n   c r e a t e s   a   t a b l e   
 
 
 
           a r g   =   {   [ - 2 ]   =   " l u a " ,   [ - 1 ]   =   " - l a " , 
 
                           [ 0 ]   =   " b . l u a " , 
 
                           [ 1 ]   =   " t 1 " ,   [ 2 ]   =   " t 2 "   } 
 
 
 
 a n d   f i n a l l y   r u n s   t h e   f i l e   b . l u a .   T h e   s c r i p t   i s   c a l l e d   w i t h   a r g [ 1 ] ,   a r g [ 2 ] ,        a s   a r g u m e n t s ;   i t   c a n   a l s o   a c c e s s   t h e s e   
 
 a r g u m e n t s   w i t h   t h e   v a r a r g   e x p r e s s i o n   ' . . . ' .   
 
 
 
 I n   i n t e r a c t i v e   m o d e ,   i f   y o u   w r i t e   a n   i n c o m p l e t e   s t a t e m e n t ,   t h e   i n t e r p r e t e r   w a i t s   f o r   i t s   c o m p l e t i o n   b y   i s s u i n g   a   d i f f e r e n t   
 
 p r o m p t .   
 
 
 
 I f   t h e   g l o b a l   v a r i a b l e   _ P R O M P T   c o n t a i n s   a   s t r i n g ,   t h e n   i t s   v a l u e   i s   u s e d   a s   t h e   p r o m p t .   S i m i l a r l y ,   i f   t h e   g l o b a l   
 
 v a r i a b l e   _ P R O M P T 2   c o n t a i n s   a   s t r i n g ,   i t s   v a l u e   i s   u s e d   a s   t h e   s e c o n d a r y   p r o m p t   ( i s s u e d   d u r i n g   i n c o m p l e t e   s t a t e m e n t s ) .   
 
 T h e r e f o r e ,   b o t h   p r o m p t s   c a n   b e   c h a n g e d   d i r e c t l y   o n   t h e   c o m m a n d   l i n e   o r   i n   a n y   L u a   p r o g r a m s   b y   a s s i g n i n g   t o   _ P R O M P T .   S e e   t h e   
 
 n e x t   e x a m p l e :   
 
 
 
           $   l u a   - e " _ P R O M P T = ' m y p r o m p t >   ' "   - i 
 
 
 
 ( T h e   o u t e r   p a i r   o f   q u o t e s   i s   f o r   t h e   s h e l l ,   t h e   i n n e r   p a i r   i s   f o r   L u a . )   N o t e   t h e   u s e   o f   - i   t o   e n t e r   i n t e r a c t i v e   m o d e ;   
 
 o t h e r w i s e ,   t h e   p r o g r a m   w o u l d   j u s t   e n d   s i l e n t l y   r i g h t   a f t e r   t h e   a s s i g n m e n t   t o   _ P R O M P T .   
 
 
 
 T o   a l l o w   t h e   u s e   o f   L u a   a s   a   s c r i p t   i n t e r p r e t e r   i n   U n i x   s y s t e m s ,   t h e   s t a n d - a l o n e   i n t e r p r e t e r   s k i p s   t h e   f i r s t   l i n e   o f   a   
 
 c h u n k   i f   i t   s t a r t s   w i t h   # .   T h e r e f o r e ,   L u a   s c r i p t s   c a n   b e   m a d e   i n t o   e x e c u t a b l e   p r o g r a m s   b y   u s i n g   c h m o d   + x   a n d   t h e   # !   f o r m ,   
 
 a s   i n   
 
 
 
           # ! / u s r / l o c a l / b i n / l u a 
 
 
 
 ( O f   c o u r s e ,   t h e   l o c a t i o n   o f   t h e   L u a   i n t e r p r e t e r   m a y   b e   d i f f e r e n t   i n   y o u r   m a c h i n e .   I f   l u a   i s   i n   y o u r   P A T H ,   t h e n   
 
 
 
           # ! / u s r / b i n / e n v   l u a 
 
 
 
 i s   a   m o r e   p o r t a b l e   s o l u t i o n . )   
 
 
 
 7   -   I n c o m p a t i b i l i t i e s   w i t h   t h e   P r e v i o u s   V e r s i o n 
 
 H e r e   w e   l i s t   t h e   i n c o m p a t i b i l i t i e s   t h a t   y o u   m a y   f i n d   w h e n   m o v i n g   a   p r o g r a m   f r o m   L u a   5 . 0   t o   L u a   5 . 1 .   Y o u   c a n   a v o i d   m o s t   o f   
 
 t h e   i n c o m p a t i b i l i t i e s   c o m p i l i n g   L u a   w i t h   a p p r o p r i a t e   o p t i o n s   ( s e e   f i l e   l u a c o n f . h ) .   H o w e v e r ,   a l l   t h e s e   c o m p a t i b i l i t y   o p t i o n s   
 
 w i l l   b e   r e m o v e d   i n   t h e   n e x t   v e r s i o n   o f   L u a .   
 
 
 
 7 . 1   -   C h a n g e s   i n   t h e   L a n g u a g e 
 
 T h e   v a r a r g   s y s t e m   c h a n g e d   f r o m   t h e   p s e u d o - a r g u m e n t   a r g   w i t h   a   t a b l e   w i t h   t h e   e x t r a   a r g u m e n t s   t o   t h e   v a r a r g   e x p r e s s i o n .   
 
 ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ V A R A R G   i n   l u a c o n f . h . )   
 
 T h e r e   w a s   a   s u b t l e   c h a n g e   i n   t h e   s c o p e   o f   t h e   i m p l i c i t   v a r i a b l e s   o f   t h e   f o r   s t a t e m e n t   a n d   f o r   t h e   r e p e a t   s t a t e m e n t .   
 
 T h e   l o n g   s t r i n g / l o n g   c o m m e n t   s y n t a x   ( [ [ s t r i n g ] ] )   d o e s   n o t   a l l o w   n e s t i n g .   Y o u   c a n   u s e   t h e   n e w   s y n t a x   ( [ = [ s t r i n g ] = ] )   i n   t h e s e   
 
 c a s e s .   ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ L S T R   i n   l u a c o n f . h . )   
 
 7 . 2   -   C h a n g e s   i n   t h e   L i b r a r i e s 
 
 F u n c t i o n   s t r i n g . g f i n d   w a s   r e n a m e d   s t r i n g . g m a t c h .   ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ G F I N D   i n   l u a c o n f . h . )   
 
 W h e n   s t r i n g . g s u b   i s   c a l l e d   w i t h   a   f u n c t i o n   a s   i t s   t h i r d   a r g u m e n t ,   w h e n e v e r   t h i s   f u n c t i o n   r e t u r n s   n i l   o r   f a l s e   t h e   
 
 r e p l a c e m e n t   s t r i n g   i s   t h e   w h o l e   m a t c h ,   i n s t e a d   o f   t h e   e m p t y   s t r i n g .   
 
 F u n c t i o n   t a b l e . s e t n   w a s   d e p r e c a t e d .   F u n c t i o n   t a b l e . g e t n   c o r r e s p o n d s   t o   t h e   n e w   l e n g t h   o p e r a t o r   ( # ) ;   u s e   t h e   o p e r a t o r   
 
 i n s t e a d   o f   t h e   f u n c t i o n .   ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ G E T N   i n   l u a c o n f . h . )   
 
 F u n c t i o n   l o a d l i b   w a s   r e n a m e d   p a c k a g e . l o a d l i b .   ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ L O A D L I B   i n   l u a c o n f . h . )   
 
 F u n c t i o n   m a t h . m o d   w a s   r e n a m e d   m a t h . f m o d .   ( S e e   c o m p i l e - t i m e   o p t i o n   L U A _ C O M P A T _ M O D   i n   l u a c o n f . h . )   
 
 F u n c t i o n s   t a b l e . f o r e a c h   a n d   t a b l e . f o r e a c h i   a r e   d e p r e c a t e d .   Y o u   c a n   u s e   a   f o r   l o o p   w i t h   p a i r s   o r   i p a i r s   i n s t e a d .   
 
 T h e r e   w e r e   s u b s t a n t i a l   c h a n g e s   i n   f u n c t i o n   r e q u i r e   d u e   t o   t h e   n e w   m o d u l e   s y s t e m .   H o w e v e r ,   t h e   n e w   b e h a v i o r   i s   m o s t l y   
 
 c o m p a t i b l e   w i t h   t h e   o l d ,   b u t   r e q u i r e   g e t s   t h e   p a t h   f r o m   p a c k a g e . p a t h   i n s t e a d   o f   f r o m   L U A _ P A T H .   
 
 F u n c t i o n   c o l l e c t g a r b a g e   h a s   d i f f e r e n t   a r g u m e n t s .   F u n c t i o n   g c i n f o   i s   d e p r e c a t e d ;   u s e   c o l l e c t g a r b a g e ( " c o u n t " )   i n s t e a d .   
 
 7 . 3   -   C h a n g e s   i n   t h e   A P I 
 
 T h e   l u a o p e n _ *   f u n c t i o n s   ( t o   o p e n   l i b r a r i e s )   c a n n o t   b e   c a l l e d   d i r e c t l y ,   l i k e   a   r e g u l a r   C   f u n c t i o n .   T h e y   m u s t   b e   c a l l e d   
 
 t h r o u g h   L u a ,   l i k e   a   L u a   f u n c t i o n .   
 
 F u n c t i o n   l u a _ o p e n   w a s   r e p l a c e d   b y   l u a _ n e w s t a t e   t o   a l l o w   t h e   u s e r   t o   s e t   a   m e m o r y - a l l o c a t i o n   f u n c t i o n .   Y o u   c a n   u s e   
 
 l u a L _ n e w s t a t e   f r o m   t h e   s t a n d a r d   l i b r a r y   t o   c r e a t e   a   s t a t e   w i t h   a   s t a n d a r d   a l l o c a t i o n   f u n c t i o n   ( b a s e d   o n   r e a l l o c ) .   
 
 F u n c t i o n s   l u a L _ g e t n   a n d   l u a L _ s e t n   ( f r o m   t h e   a u x i l i a r y   l i b r a r y )   a r e   d e p r e c a t e d .   U s e   l u a _ o b j l e n   i n s t e a d   o f   l u a L _ g e t n   a n d   
 
 n o t h i n g   i n s t e a d   o f   l u a L _ s e t n .   
 
 F u n c t i o n   l u a L _ o p e n l i b   w a s   r e p l a c e d   b y   l u a L _ r e g i s t e r .   
 
 F u n c t i o n   l u a L _ c h e c k u d a t a   n o w   t h r o w s   a n   e r r o r   w h e n   t h e   g i v e n   v a l u e   i s   n o t   a   u s e r d a t a   o f   t h e   e x p e c t e d   t y p e .   ( I n   L u a   5 . 0   i t   
 
 r e t u r n e d   N U L L . )   
 
 8   -   T h e   C o m p l e t e   S y n t a x   o f   L u a 
 
 H e r e   i s   t h e   c o m p l e t e   s y n t a x   o f   L u a   i n   e x t e n d e d   B N F .   ( I t   d o e s   n o t   d e s c r i b e   o p e r a t o r   p r e c e d e n c e s . )   
 
 
 
 
 
 	 c h u n k   : : =   { s t a t   [ ` ;  ] }   [ l a s t s t a t   [ ` ;  ] ] 
 
 
 
 	 b l o c k   : : =   c h u n k 
 
 
 
 	 s t a t   : : =     v a r l i s t   ` =    e x p l i s t   |   
 
 	 	   f u n c t i o n c a l l   |   
 
 	 	   d o   b l o c k   e n d   |   
 
 	 	   w h i l e   e x p   d o   b l o c k   e n d   |   
 
 	 	   r e p e a t   b l o c k   u n t i l   e x p   |   
 
 	 	   i f   e x p   t h e n   b l o c k   { e l s e i f   e x p   t h e n   b l o c k }   [ e l s e   b l o c k ]   e n d   |   
 
 	 	   f o r   N a m e   ` =    e x p   ` ,    e x p   [ ` ,    e x p ]   d o   b l o c k   e n d   |   
 
 	 	   f o r   n a m e l i s t   i n   e x p l i s t   d o   b l o c k   e n d   |   
 
 	 	   f u n c t i o n   f u n c n a m e   f u n c b o d y   |   
 
 	 	   l o c a l   f u n c t i o n   N a m e   f u n c b o d y   |   
 
 	 	   l o c a l   n a m e l i s t   [ ` =    e x p l i s t ]   
 
 
 
 	 l a s t s t a t   : : =   r e t u r n   [ e x p l i s t ]   |   b r e a k 
 
 
 
 	 f u n c n a m e   : : =   N a m e   { ` .    N a m e }   [ ` :    N a m e ] 
 
 
 
 	 v a r l i s t   : : =   v a r   { ` ,    v a r } 
 
 
 
 	 v a r   : : =     N a m e   |   p r e f i x e x p   ` [    e x p   ` ]    |   p r e f i x e x p   ` .    N a m e   
 
 
 
 	 n a m e l i s t   : : =   N a m e   { ` ,    N a m e } 
 
 
 
 	 e x p l i s t   : : =   { e x p   ` ,  }   e x p 
 
 
 
 	 e x p   : : =     n i l   |   f a l s e   |   t r u e   |   N u m b e r   |   S t r i n g   |   ` . . .    |   f u n c t i o n   |   
 
 	 	   p r e f i x e x p   |   t a b l e c o n s t r u c t o r   |   e x p   b i n o p   e x p   |   u n o p   e x p   
 
 
 
 	 p r e f i x e x p   : : =   v a r   |   f u n c t i o n c a l l   |   ` (    e x p   ` )  
 
 
 
 	 f u n c t i o n c a l l   : : =     p r e f i x e x p   a r g s   |   p r e f i x e x p   ` :    N a m e   a r g s   
 
 
 
 	 a r g s   : : =     ` (    [ e x p l i s t ]   ` )    |   t a b l e c o n s t r u c t o r   |   S t r i n g   
 
 
 
 	 f u n c t i o n   : : =   f u n c t i o n   f u n c b o d y 
 
 
 
 	 f u n c b o d y   : : =   ` (    [ p a r l i s t ]   ` )    b l o c k   e n d 
 
 
 
 	 p a r l i s t   : : =   n a m e l i s t   [ ` ,    ` . . .  ]   |   ` . . .  
 
 
 
 	 t a b l e c o n s t r u c t o r   : : =   ` {    [ f i e l d l i s t ]   ` }  
 
 
 
 	 f i e l d l i s t   : : =   f i e l d   { f i e l d s e p   f i e l d }   [ f i e l d s e p ] 
 
 
 
 	 f i e l d   : : =   ` [    e x p   ` ]    ` =    e x p   |   N a m e   ` =    e x p   |   e x p 
 
 
 
 	 f i e l d s e p   : : =   ` ,    |   ` ;  
 
 
 
 	 b i n o p   : : =   ` +    |   ` -    |   ` *    |   ` /    |   ` ^    |   ` %    |   ` . .    |   
 
 	 	   ` <    |   ` < =    |   ` >    |   ` > =    |   ` = =    |   ` ~ =    |   
 
 	 	   a n d   |   o r 
 
 
 
 	 u n o p   : : =   ` -    |   n o t   |   ` #  
 
 
 
 
 
 
 
 
 
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 